Why cant I rotate my imageview with one finger Swift?

时间:2016-04-04 18:58:05

标签: ios swift touchesmoved

So Im making a DJ app and I have this turntable that I would like to spin to the left and right using touch. I got it to move when a song is being played but not sure how to get it to spin using touch.

EDIT******* I got it to work but I need to rotate the imageview using one finger instead of two. How would I get that to work? Thank you

class ViewController: UIViewController, MPMediaPickerControllerDelegate {

    var angle: CGFloat = 0
    var rotate = UIRotationGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.rotate = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.rotate(_:)))
        self.leftTurnTable.addGestureRecognizer(self.rotate)
    }

    func rotate(r:UIRotationGestureRecognizer) {

        self.leftTurnTable.transform = CGAffineTransformMakeRotation(angle+r.rotation)

        if r.state == UIGestureRecognizerState.Ended {

            self.angle += r.rotation
        }
    }
} 

1 个答案:

答案 0 :(得分:2)

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {


     let touch = touches.first
    if touch!.view === leftTurnTable {
        let position = touch!.locationInView(self.view)
        let target = leftTurnTable.center
        let angle = atan2(target.y-position.y, target.x-position.x)
        leftTurnTable.transform = CGAffineTransformMakeRotation(angle)
    }
}