Using multiple UIGestureRecognizers simultaneously like UIRotationGestureRecognizer & UIPanGestureRecognizer in Swift 3

无人久伴 提交于 2019-12-04 19:19:29

The key to implementing multiple gesture recognisers simultaneously is modifying their CGAffineTransforms rather than overwriting them.

Apple > Documentation > Core Graphics > CGAffineTransform:

Note that you do not typically need to create affine transforms directly. If you want only to draw an object that is scaled or rotated, for example, it is not necessary to construct an affine transform to do so. The most direct way to manipulate your drawing—whether by movement, scaling, or rotation—is to call the functions translateBy(x:y:) , scaleBy(x:y:) , or rotate(by:) , respectively. You should generally only create an affine transform if you want to reuse it later.

Furthermore, when changes are detected, after applying the translation, it is important to reset the value of the sender, so that the translations do not compound each time they are detected.

For example:

@IBAction func rotateAction(_ sender: UIRotationGestureRecognizer) {
    guard let view = sender.view else { return }

    switch sender.state {
    case .changed:
        view.transform = view.transform.rotated(by: sender.rotation)
        sender.rotation = 0
    default: break
    }
}

and

@IBAction func panAction(_ sender: UIPanGestureRecognizer) {
    guard let view = sender.view else { return }

    switch sender.state {
    case .changed:
        let translationX = sender.translation(in: view).x
        let translationY = sender.translation(in: view).y

        view.transform = view.transform.translatedBy(x: translationX, y: translationY)
        sender.setTranslation(CGPoint.zero, in: view)
    default: break
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!