Calculate the rotation speed and naturally rotate the iOS view

I have a round shape and rotate it with the following code overriding touchesBegan(touches:, withEvent:) and touchesMoved(touches:, withEvent:) .

 var deltaAngle = CGFloat(0) var startTransform: CGAffineTransform? override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touchPoint = touches.first!.locationInView(view) let dx = touchPoint.x - view.center.x let dy = touchPoint.y - view.center.y deltaAngle = atan2(dy, dx) startTransform = arrowPickerView.transform } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { let touchPoint = touches.first!.locationInView(view) let dx = touchPoint.x - view.center.x let dy = touchPoint.y - view.center.y let angle = atan2(dy, dx) let angleDifference = deltaAngle - angle let transform = CGAffineTransformRotate(startTransform!, -angleDifference) arrowPickerView.transform = transform } 

I want to override touchesEnded(touches:, withEvent:) to calculate the speed and take a look at the view (like continuous scrolling). I am currently saving the original transformation and calculating the delta angle. How can i implement this? Thanks in advance!

+7
ios swift trigonometry transform touch-event
source share
1 answer

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.

+2
source share

All Articles