ios - 使用 3D touch peek and pop,您如何检测用户在 peek 期间的平移(如 Facebook)?

标签 ios objective-c uiviewcontroller ios9 3dtouch

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

这是如何在 API 中完成的?我所看到的只是提供一个 View Controller 并在弹出时提交该 View Controller 。

最佳答案

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

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

由于您可以访问 Peeked View Controller ,因此您可以从该 View Controller 调用与用户的触摸位置相关的函数。

只是一个快速的模型:

@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

关于ios - 使用 3D touch peek and pop,您如何检测用户在 peek 期间的平移(如 Facebook)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36320488/

相关文章:

IOS:从数组中将字符串写入文件txt

iphone - OpenCV.Framework 不为 armv7s 架构编译

ios 无法识别的选择器发送到实例

objective-c - 在 CocoaPods post_install 步骤中运行 bash 命令

ios - 在上下文中绘制 View ,这是什么意思?

ios - SWRevealViewController 在 ViewController 之间切换

ios - 在 Swift 3.0 中实现没有导航栏的滑动返回导航

ios - 当目标为 7.0 时,Xcode 引发 "EXC_BAD_ACCESS code=1"

ios - 带有 SubView 的 tableView 的容器 View 或 UIViewController

ios - viewDidload 和 viewDidAppear 没有被调用?