使用UIPanGestureRecognizer-Swift 3旋转ImageView

时间:2017-03-21 18:53:51

标签: ios swift xcode swift3 uigesturerecognizer

我正在尝试旋转我拥有的ImageView,具体取决于它所在的X坐标。基本上,我希望它在x = 300时旋转0º,在x = 190时旋转180º。

我必须以编程方式编写UIPanGestureRecognizer。这是我现在的代码:

    @objc func personDrag(recognizer: UIPanGestureRecognizer) {

    let rotationSub: CGFloat = 1

    let translation = recognizer.translation(in: rView)
    if let view = recognizer.view {
        view.center = CGPoint(x:view.center.x +  translation.x, y:view.center.y + translation.y)
        view.transform = view.transform.rotated(by: CGFloat.pi - rotationSub)
    }
    recognizer.setTranslation(CGPoint.zero, in: rView)

}

我每次平移时都会尝试将旋转度改为1,但它并没有真正起作用/有意义。任何帮助,将不胜感激。非常感谢你!

干杯,Theo

2 个答案:

答案 0 :(得分:3)

您可以在此基础上构建实现:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageview: UIImageView!

    private var currentRotation: Rotation = .none

    /* Certain rotation points (rotation of 0º when x = 300 and a rotation of 180º when x = 190) */
    enum Rotation {
        case none, xPoint190, xPoint300
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
        imageview.addGestureRecognizer(gestureRecognizer)
        imageview.isUserInteractionEnabled = true
    }

    @IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
        guard gestureRecognizer.state == .began || gestureRecognizer.state == .changed else {
            return
        }

        guard let imgView = gestureRecognizer.view else {
            return
        }

        let translation = gestureRecognizer.translation(in: self.view)
        imgView.center = CGPoint(x: imgView.center.x + translation.x, y: imgView.center.y + translation.y)
        gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)

        let angle: CGFloat = self.degreesToRadians(180.0)

        /* After reaching x point case - rotating and setting rotation occured to prohibit further rotation */

        if imgView.layer.frame.origin.x <= 190, currentRotation != .xPoint190 {

        imgView.transform = imgView.transform.rotated(by: angle)
        currentRotation = .xPoint190

    } else if imgView.layer.frame.origin.x >= 300, currentRotation != .xPoint300 {

        imgView.transform = imgView.transform.rotated(by: angle)
        currentRotation = .xPoint300
    }


    private func degreesToRadians(_ deg: CGFloat) -> CGFloat {
        return deg * CGFloat.pi / 180
    }
}

答案 1 :(得分:0)

希望这会对您有所帮助。

 @objc func rotateViewPanGesture(_ recognizer: UIPanGestureRecognizer) {
            touchLocation = recognizer.location(in: superview)
            let center = CGRectGetCenter(frame)

            switch recognizer.state {
            case .began:
                deltaAngle = atan2(touchLocation!.y - center.y, touchLocation!.x - center.x) - CGAffineTrasformGetAngle(transform)
                initialBounds = bounds
                initialDistance = CGpointGetDistance(center, point2: touchLocation!)

            case .changed:

                let ang = atan2(touchLocation!.y - center.y, touchLocation!.x - center.x)
                let angleDiff = deltaAngle! - ang

                let a = transform.a
                let b = transform.b
                let c = transform.c
                let d = transform.d

                let sx = sqrt(a * a + b * b)
                let sy = sqrt(c * c + d * d)

                let currentScale = CGPoint(x: sx, y: sy)
                let scale = CGAffineTransform(scaleX: currentScale.x, y: currentScale.y)
                self.transform = scale.rotated(by: -angleDiff)

                layoutIfNeeded()
            case .ended:

              print("end gesture status")
            default:break

            }
        }