Swift 4-设置平移手势速度以旋转Imageview

时间:2018-10-03 07:26:21

标签: ios swift xcode swift4

到目前为止,我已经尝试了以下代码,但这仅旋转Imageview一次,我要寻找的是如果用户轻轻滑动图像会稍微旋转并停止,并且如果用户用力滑动图像会旋转一次,两次并因此取决于所刷的速度现在,from和to值是静态的。如何在此功能中设置平移手势速度?

@objc func gestureRotate(gesture : UIPanGestureRecognizer) {
    let velocity = gesture.velocity(in: self.imgViw)
    if velocity.x > 0 {
        if gesture.state == .began{
            startingPoint = gesture.velocity(in: self.imgViw)
            print("Starting point: \(startingPoint)")
        }
        if gesture.state == .ended{
            let endingPoint = gesture.velocity(in: self.imgViw)
            print("Ending point: \(endingPoint)")
        }
        print("Pan right")
        UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
            self.imgViw.rotate360Degrees()
        }) { (value: Bool) in
            UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
                self.imgViw.transform = .identity
            }, completion: nil)
        }
    }else{
        print("Pan left")
        UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
            self.imgViw.antiClockwiseRotate360Degrees()
        }) { (value: Bool) in
            UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
                self.imgViw.transform = .identity
            }, completion: nil)
        }
    }

}

extension UIView {
    func rotate360Degrees(duration: CFTimeInterval = 0.5, completionDelegate: AnyObject? = nil) {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = 360 * CGFloat(Double.pi / 180)
        rotateAnimation.duration = duration
        if let delegate: CAAnimationDelegate = completionDelegate as! CAAnimationDelegate? {
            rotateAnimation.delegate = delegate
        }
        self.layer.add(rotateAnimation, forKey: nil)
    }
}

0 个答案:

没有答案