UIPinchGestureRecognizer 和 UIPanGestureRecognizer 可以合并吗?
我正在努力弄清楚是否可以创建一个将 UIPinchGestureRecognizer 与 UIPanGestureRecognizer 结合起来的单个组合手势识别器。
我使用平移进行视图平移,使用捏合进行视图缩放。我正在进行增量矩阵串联,以导出应用于视图的最终转换矩阵。这个矩阵既有尺度又有平移。使用单独的手势识别器会导致抖动/缩放。不是我想要的。因此,我想在一个手势中处理一次缩放和平移的串联。有人可以阐明如何做到这一点吗?
I am struggling a bit trying to figure out if it is possible to create a single combined gesture recognizer that combines UIPinchGestureRecognizer with UIPanGestureRecognizer.
I am using pan for view translation and pinch for view scaling. I am doing incremental matrix concatenation to derive a resultant final transformation matrix that is applied to the view. This matrix has both scale and translation. Using separate gesture recognizers leads to a jittery movement/scaling. Not what I want. Thus, I want to handle concatenation of scale and translation once within a single gesture. Can someone please shed some light on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
2014 年 6 月 14 日:使用 ARC 更新了 iOS 7+ 的示例代码。
UIGestureRecognizer 可以一起工作,您只需要确保不会破坏当前视图的变换矩阵。使用 CGAffineTransformScale 方法和相关方法将变换作为输入,而不是从头开始创建它(除非您自己维护当前的旋转、缩放或平移。
下载 Xcode 项目
注意:对于应用了平移/捏合/旋转手势的 IB 中的 UIView,iOS 7 的行为很奇怪。iOS 8 修复了这个问题,但我的解决方法是在代码中添加所有视图,如此代码示例
将它们添加到视图中并符合 UIGestureRecognizerDelegate 协议
实现手势方法
资源
6/14/14: Updated Sample Code for iOS 7+ with ARC.
The UIGestureRecognizers can work together and you just need to make sure you don't trash the current view's transform matrix. Use the CGAffineTransformScale method and related methods that take a transform as input, rather than creating it from scratch (unless you maintain the current rotation, scale, or translation yourself.
Download Xcode Project
Note: iOS 7 behaves weird with UIView's in IB that have Pan/Pinch/Rotate gestures applied. iOS 8 fixes it, but my workaround is to add all views in code like this code example.
Demo Video
Add them to a view and conform to the UIGestureRecognizerDelegate protocol
Implement gesture methods
Resources
如果有人对使用 Metal 进行渲染的 Swift 实现感兴趣,我有一个项目这里。
If anyone is interested in a Swift implementation of this using Metal to do the rendering, I have a project available here.
斯威夫特
非常感谢保罗!这是他的 Swift 版本:
演示(在模拟器上):
Swift
Many thanks a lot to Paul!!! Here is his Swift version:
Demo (on Simulator):