为什么我不能用一根手指旋转我的 imageview Swift?
Why cant I rotate my imageview with one finger Swift?
所以我正在制作一个 DJ 应用程序,并且我有一个转盘,我想通过触摸向左和向右旋转。我让它在播放歌曲时移动,但不确定如何使用触摸让它旋转。
编辑*******
我让它工作,但我需要用一根手指而不是两根手指来旋转图像视图。我怎样才能让它发挥作用?谢谢
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)
}
}
所以我正在制作一个 DJ 应用程序,并且我有一个转盘,我想通过触摸向左和向右旋转。我让它在播放歌曲时移动,但不确定如何使用触摸让它旋转。
编辑******* 我让它工作,但我需要用一根手指而不是两根手指来旋转图像视图。我怎样才能让它发挥作用?谢谢
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)
}
}