您的位置:首页 > 运维架构

利用 3D touch 的“Peek and Pop”实现视频预览

2015-10-20 14:47 375 查看
iOS9 和 iPhone6s/6s Plus 的发布,比较令人关注的一个新特性就是 3D touch 。

效果视频页: http://demo.polyv.net/data/touch.html

本案在视频列表的 UITableView 里面,用 3D touch 的 Peek and Pop 实现了点开视频播放的详情 DetailViewController 之前,先做个视频预览。

事先创建

@interface MainViewController : UITableViewController

实现 Table view data source 加载一些演示用的视频列表。

在 viewDidLoad 方法里面,判断当前使用设备是否支持 3D touch

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {

[self registerForPreviewingWithDelegate:(id)self sourceView:self.view];

}

这一步通过 registerForPreviewingWithDelegate 方法,将当前 viewcontroller 增加预览功能。 接下来需要实现预览功能的两个代理方法:

pragma mark - 3D Touch Delegate

(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{

//通过当前点按的位置拿到当前 tableview 的行号,此处实现 peek 动作

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

Video *video = [_videolist objectAtIndex:indexPath.row];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:

@"Main" bundle:[NSBundle mainBundle]];

DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"detailViewController"];

//传递当前选择的视频信息

detailViewController.video = video;

//获取当前选择的表单元格

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

//预览窗口只截取详情视频 detailViewController 的上半部分,高度设置为 240

detailViewController.preferredContentSize =(CGSize){0, 240};

?
//除了这个单元格,需要模糊其它位置

previewingContext.sourceRect = cell.frame;

return detailViewController;

}

(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController
*)viewControllerToCommit {

//继续按进来则实现 pop 的动作,弹出详情页

?
?
[self showViewController:viewControllerToCommit sender:self];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: