I need to rotate an UIImageview on user\'s motion using just one finger motion. For that I use a rotation gesture and then override touchesMoved with this code:
you may want to change self.superview to self.view (mine is subclass)
var xOffSet:CGVector = CGVectorMake(0, 0)
var yOffSet:CGVector = CGVectorMake(0, 0)
var origin:CGPoint = CGPointZero
var tempTransform=CGAffineTransform()
var startingAngle:CGFloat?
override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
origin = (touches.first?.locationInView(self.superview))!
xOffSet = CGVector(dx:(origin.x)-self.center.x, dy:(origin.y) - self.center.y)
startingAngle = atan2(xOffSet.dy,xOffSet.dx)
//save the current transform
tempTransform = self.transform
}
override func touchesMoved(touches: Set, withEvent event: UIEvent?) {
let touchPoint = touches.first?.locationInView(self.superview)
yOffSet = CGVector(dx:touchPoint!.x - self.center.x, dy:touchPoint!.y - self.center.y)
let angle = atan2(yOffSet.dy,yOffSet.dx)
let deltaAngle = angle - startingAngle!
self.transform = CGAffineTransformRotate(tempTransform, deltaAngle)
}
override func touchesEnded(touches: Set, withEvent event: UIEvent?) {
startingAngle = nil
}
//reset in case drag is cancelled
override func touchesCancelled(touches: Set?, withEvent event: UIEvent?) {
self.transform = tempTransform
startingAngle = nil
}