In my example below, CGAffineTransformRotate depends on:
- (if the user moves from left to right, up or down, etc.)
- rotation (time-dependent factor between Began and touchEnded touches)
- PI
This creates a CGAffineTransformRotate, and it rotates with a duration of 1 (s) to create a kinetic feel for the UIViewAnimationOptions.CurveEaseOut property used
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { let touchPointEnd = touches.first!.locationInView(view) self.dateTouchesEnded = NSDate() let delay : NSTimeInterval = NSTimeInterval(0) let timeDelta : Double = self.dateTouchesEnded!.timeIntervalSince1970 - self.dateTouchesStarted!.timeIntervalSince1970 let horizontalDistance : Double = Double(touchPointEnd.x - self.touchPointStart!.x) let verticalDistance : Double = Double(touchPointEnd.y - self.touchPointStart!.y) var direction : Double = 1 if (fabs(horizontalDistance) > fabs(verticalDistance)) { if horizontalDistance > 0 { direction = -1 } } else { if verticalDistance < 0 { direction = -1 } } let rotation : Double = (0.1 / timeDelta < 0.99) ? 0.1 / timeDelta : 0.99 let duration : NSTimeInterval = NSTimeInterval(1) UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseOut, animations: { let transform = CGAffineTransformRotate(self.imageView.transform, CGFloat(direction) * CGFloat(rotation) * CGFloat(M_PI)) self.imageView.transform = transform }, completion: nil) }
I added variables to track the start and end time of touches and added the CGPoint location of the touchhesBegan function
var dateTouchesStarted : NSDate? var dateTouchesEnded : NSDate? var touchPointStart : CGPoint?
In Began contacts, I added:
self.touchPointStart = touchPoint
To set a variable as described above.
Emptyless
source share