V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lyver
V2EX  ›  iPhone

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

  •  
  •   lyver · 2015-10-19 18:34:46 +08:00 · 4220 次点击
    这是一个创建于 3113 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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];

    }

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1152 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 18:01 · PVG 02:01 · LAX 11:01 · JFK 14:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.