在 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/