如何实现UIGestureRecognizerDelegate?

时间:2017-02-23 01:07:47

标签: swift3 uigesturerecognizer

这是我的代码,但它无效。

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func handlePan(recognizer:UIPanGestureRecognizer){
        recognizer.delegate = self

        let translation = recognizer.translation(in: self.view)

        if let view = recognizer.view{
            view.center = CGPoint(x:view.center.x + translation.x,
                                  y:view.center.y + translation.y)
        }
    recognizer.setTranslation(CGPoint.zero, in: self.view)

        if recognizer.state == UIGestureRecognizerState.ended{
            let velocity = recognizer.velocity(in: self.view)
            let magnitude = sqrt((velocity.x * velocity.x)+(velocity.y * velocity.y))
            let slideMultiplier = magnitude / 600

            let slideFactor = 0.1 * slideMultiplier

            var finalPoint = CGPoint(x:recognizer.view!.center.x + (velocity.x * slideFactor),
                                     y:recognizer.view!.center.y + (velocity.y * slideFactor))

            finalPoint.x = min(max(finalPoint.x, 0), self.view.bounds.size.width)
            finalPoint.y = min(max(finalPoint.y, 0), self.view.bounds.size.height)

            UIView.animate(withDuration: Double(slideFactor * 2),
                           delay: 0,
                           options: UIViewAnimationOptions.curveEaseOut,
                           animations: {recognizer.view!.center = finalPoint},
                           completion: nil)
        }

    }

    @IBAction func handlePinch(recognizer : UIPinchGestureRecognizer){
        recognizer.delegate = self
        if let view = recognizer.view {
           let pinch = recognizer.scale
            view.transform = CGAffineTransform.init(scaleX: pinch, y: pinch)
        }
    }

    @IBAction func handleRotate(recognizer : UIRotationGestureRecognizer){
        recognizer.delegate = self
        if let view = recognizer.view{
            let rotation = recognizer.rotation
                view.transform = CGAffineTransform.init(rotationAngle: rotation)
        }
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool{
        return true
    }
}

我在我的iPhone上尝试它,但当我尝试同时捏和旋转时它不起作用。

1 个答案:

答案 0 :(得分:0)

我发现如何做到这一点:

@IBAction func handlePinch(recognizer : UIPinchGestureRecognizer){
    if let view = recognizer.view {
       view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
        recognizer.scale = 1
    }
}

@IBAction func handleRotate(recognizer : UIRotationGestureRecognizer){
    if let view = recognizer.view{
            view.transform = view.transform.rotated(by: recognizer.rotation)
        recognizer.rotation = 0
    }
}