CGAffineTransformMakeRotation counterclockwise always

I am trying to animate a UIImageView to rotate counterclockwise when the user touches it and moves a finger on it clockwise.
Basically, I want the UIImageView to return to its original position after the touch ends, and it should move counterclockwise.

The problem is that each time angle (in degrees) is greater than 180, then the image rotates clockwise back to its original position. Apparently, he takes the shortest path back. As for angles of 179 degrees or less, the image rotates counterclockwise (which I need).

I tried both of these code snippets and they behave the same. Any suggestions?

NOTE: here the angle variable is double and it is initialized to zero, and it remains 0 in all my code

 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self handleObject:touches withEvent:event isLast:YES]; /* this doesn't make rotation counter clockwise for rotations greater than 180 */ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationCurve: UIViewAnimationCurveLinear]; [UIView setAnimationDuration:1.0]; [UIView setAnimationRepeatCount:1]; CGAffineTransform transform = CGAffineTransformMakeRotation(angle); circle1ImgVw.transform = CGAffineTransformRotate(transform, angle); // ends animation [UIView commitAnimations]; /* this doesn't make rotation counter clockwise for rotations greater than 180 */ [UIView animateWithDuration:1.0 animations:^{ circle1ImgVw.transform = CGAffineTransformMakeRotation(angle); }]; } 

I tried to see this post and there are a couple of problems with it

  • After the animation, I can no longer touch and rotate the image.
  • It is always animated clockwise, and I could not figure out how to make it smooth counterclockwise from the point where the user finished rotating the image.
+8
ios iphone xcode ipad
source share
2 answers

I had a similar problem, check the accepted answer, this may help you:

Turn the UIView clockwise for an angle of more than 180 degrees


In response to the comments, perhaps this will help:

In my code, I actually use

 rotationAnimation.fromValue rotationAnimation.toValue 

instead

 rotationAnimation.byValue. 

The rotation will always be counterclockwise if the value of toValue is less than the value of fromValue. It doesn't matter if your values ​​are positive or negative, only the relationship between them.

Find out what your starting angle is, find out which direction you want to turn, and find out what your final angle is. Make sure it is smaller than your starting angle if you want to go counterclockwise. Here is the code that I use to animate clockwise from 12:00 to the current time.

 -(void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self updateClockArmAngle]; } - (void)updateClockArmAngle { // Get the time NSDate *date = [NSDate date]; NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]]; NSDateComponents* components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date]; CGFloat hour = [components hour]; CGFloat angle = (hour/24.0)*(2*M_PI); CGFloat minute = [components minute]; angle += (minute/(24*60))*(2*M_PI); [self rotateViewAnimated:self.clockArm withDuration:1.5 byAngle:angle]; } - (void) rotateViewAnimated:(UIView*)view withDuration:(CFTimeInterval)duration byAngle:(CGFloat)angle { CABasicAnimation *rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.fromValue = 0; rotationAnimation.toValue = [NSNumber numberWithFloat:angle]; rotationAnimation.duration = duration; rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [rotationAnimation setRemovedOnCompletion:NO]; [rotationAnimation setFillMode:kCAFillModeForwards]; [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } 
+3
source share

The technique used is to divide the rotation angle by 2 and divide the rotation into two separate animations connected by the completion: parameter completion: animateWithDuration:delay:options:animations:completion: But, since the view is rotated to its current position, you need to start from this position and β€œdisconnect” to zero, as well as try to rotate the CCW from scratch.

0
source share

All Articles