【问题标题】:With 3D touch peek and pop, how do you detect the user panning during a peek (like Facebook)?使用 3D touch peek and pop,您如何检测用户在 peek 期间的平移(如 Facebook)?
【发布时间】:2016-03-30 21:58:55
【问题描述】:

在 Facebook 应用中,如果您“偷看”相册中的一张照片,您可以水平滑动手指以转到其他照片。

这是如何在 API 中完成的?我所看到的只是提供一个视图控制器并在弹出时提交该视图控制器。

【问题讨论】:

    标签: ios objective-c uiviewcontroller ios9 3dtouch


    【解决方案1】:

    有一个小技巧可以在 Peeking 时跟踪用户的触摸位置。

    您基本上有一个手势识别器,当您开始 Peeking 时开始跟踪用户的触摸位置,并在用户弹出视图控制器或释放他/她的触摸时结束跟踪。手势识别器应添加到调出 Peek 的视图控制器的视图中。

    由于您可以访问 Peeked 视图控制器,因此您可以从该视图控制器调用与用户触摸位置相关的函数。

    只是一个快速的模型:

    @property (nonatomic, weak, nullable) ViewControllerClass *peekedVC;
    
    - (void)handleGestureRecognizer:(UIPanGestureRecognizer *)gr {
        if (peekedVC && gr.state == UIGestureRecognizerStateChanged) {
            CGPoint point = [gr locationInView:self.view];
            [peekedVC handle:point.x];
        }
    }
    
    - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
              viewControllerForLocation:(CGPoint)location {
        NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location];
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    
        [previewContext setSourceRect:cell.frame];
    
        ViewControllerClass *vc = [ViewControllerClass new];
        self.peekedVC = vc;
        return vc;
    }
    
    - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
     commitViewController:(UIViewController *)viewControllerToCommit {
        self.peekedVC = nil;
        [self showViewController:viewControllerToCommit sender:self];
    }
    

    我实际上发表了一篇博文,介绍了这里的机制: https://medium.com/behancetech/peek-pan-extending-3d-touch-f6520c38fe51#.4xz7lcm9o

    还有一个开源项目可以帮助将其集成到这里: https://github.com/adobe-behancemobile/PeekPan

    【讨论】:

    • 上述代码示例未涵盖的一个关键细节是控制器应将自己设置为UIPanGestureRecognizer 的委托,并从shouldRecognizeSimultaneouslyWithGestureRecognizer: 方法返回YES
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    相关资源
    最近更新 更多