【发布时间】:2018-02-06 20:39:44
【问题描述】:
我在 UIView 上有 2 个手势识别器(旋转和捏合)。他们独立运作良好。如果我在捏后旋转,仍然可以正常工作。但是,如果我在旋转后捏捏,捏就不起作用。
- 捏:工作。
- 旋转:工作。
- (旋转 -> 捏):不工作。
- (捏合 -> 旋转):工作。
- (捏合 -> 旋转 -> 捏合):不工作。
这是我的代码:
//gestures
lazy var zoomGesture: UIPinchGestureRecognizer = {
let zoom = UIPinchGestureRecognizer.init(target: self, action: #selector(handleZoom(_:)))
zoom.delegate = self
activeLayer.addGestureRecognizer(zoom)
return zoom
}()
lazy var rotationGesture: UIRotationGestureRecognizer = {
let rotation = UIRotationGestureRecognizer.init(target: self, action: #selector(handleRotation(_:)))
rotation.delegate = self
activeLayer.addGestureRecognizer(rotation)
return rotation
}()
//handlers
@objc func handleZoom(_ gesture: UIPinchGestureRecognizer) {
if gesture.state == .began {
if activeLayer.initialTransform == nil {
activeLayer.initialTransform = activeLayer.transform
}
} else if gesture.state == .changed {
let scale = gesture.scale
activeLayer.transform = activeLayer.initialTransform!.concatenating(CGAffineTransform.init(scaleX: scale, y: scale))
} else {
activeLayer.initialTransform = nil
}
}
@objc func handleRotation(_ gesture: UIRotationGestureRecognizer) {
if gesture.state == .began {
if activeLayer.initialTransform == nil {
activeLayer.initialTransform = activeLayer.transform
}
} else if gesture.state == .changed {
let rotation = gesture.rotation
activeLayer.transform = activeLayer.initialTransform!.concatenating(CGAffineTransform.init(rotationAngle: rotation))
} else {
activeLayer.initialTransform = nil
}
}
// delegate method
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
我做错了什么或错过了什么?谢谢。
【问题讨论】:
-
查看this question,似乎是一个类似的问题。
-
运气不好我已经查过了。
-
你是如何利用这个 activeLayer 的?
-
@iOSTeam,没什么大不了的。它是一个 UIView 子类,只有一个名为
initialTransform的变量来保存手势开始时的变换信息。
标签: ios uigesturerecognizer simultaneous