Haga rotación con un dedo en Swift 4
Creé un UIGestureRecognizer para rotar una vista con solo un dedo.
La vista gira al principio pero tan pronto como alcanza un cierto grado, la rotación gira en la otra dirección.
¿Me pueden ayudar con mi código?
UIViewcontroller <- Todo está bien aquí
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let wheel = TestWheelView()
wheel.frame = CGRect.init(x: self.view.center.x - 120, y: self.view.center.y - 120, width: 240, height: 240)
self.view.addSubview(wheel)
wheel.addGestureRecognizer(TestRotateGestureRecognizer())
}
UIGestureRecognizer <- El problema está aquí
import UIKit
class TestRotateGestureRecognizer: UIGestureRecognizer {
var previousPoint = CGPoint()
var currentPoint = CGPoint()
var startAngle = CGFloat()
var currentAngle = CGFloat()
var currentRotation = CGFloat()
var totalRotation = CGFloat()
func angleForPoint(_ point:CGPoint) -> CGFloat{
var angle = atan2(point.y - (self.view?.center.y)!, point.x - (self.view?.center.x)!)
return angle
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
if let firstTouch = touches.first {
previousPoint = firstTouch.previousLocation(in: self.view)
currentPoint = firstTouch.location(in: self.view)
startAngle = angleForPoint(previousPoint)
currentAngle = angleForPoint(currentPoint)
currentRotation = currentAngle - startAngle
totalRotation += currentRotation
self.view?.transform = CGAffineTransform(rotationAngle: totalRotation)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesEnded(touches, with: event)
}
}