简体   繁体   中英

Why cant I rotate my imageview with one finger Swift?

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
        }
    }
} 
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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM