R – change counterclockwise to clockwise with CGAffineTransformIdentity

core-animationiphone

I have a telephone wheel. On the touchend, it goes to his position with an animation.

Until the angle is less than 180°, it returns clockwise. No problem, with this code :

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 [UIView setAnimationBeginsFromCurrentState:YES];
 [UIView beginAnimations:nil context:NULL];
 [UIViewsetAnimationDuration:0.5];
 wheel.transform = CGAffineTransformIdentity;
 [UIView commitAnimations];
}

But It goes wrong after that and continue to rotate for a complet turn.

I tried to make to animations like this :

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
wheel.transform = CGAffineTransformRotate(wheel.transform, degreesToRadians(-130));
[UIView commitAnimations];
[self performSelector:@selector(animatewheelViewToCenter) withObject:nil afterDelay:0.3];
}

- (void)animatewheelViewToCenter{
 [UIView setAnimationBeginsFromCurrentState:YES];
 [UIView beginAnimations:nil context:NULL];
 [UIViewsetAnimationDuration:0.3];
 wheel.transform = CGAffineTransformIdentity;
 [UIView commitAnimations];
}

It works, but the animation isn't fluid ; the change is visible.

Best Answer

I'm not sure what the state is when touchesEnded (in terms of rotation) and I'm assuing you chose degreesToRadians(-130) to try and do it partially and expect the next method to do the rest. This should be a better solution that hopefully yields the result you're expecting. I'm not sure what cadran is or why you're rotating that, so I'll just rotate the wheel.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    wheel.transform = CGAffineTransformMakeRotation(degreesToRadians(-130));
    // I would recommend that the degrees you rotate be half of whatever your rotation is.
    // this would make the wheel rotate to the home position more evenly
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animatewheelViewToCenter:finished:context:)];
    [UIView commitAnimations];
}

- (void)animatewheelViewToCenter:(NSString *)animationID finished:(NSNumber *)finished context:(id)context {
    [UIView setAnimationBeginsFromCurrentState:YES]; // you might need to make this NO
    [UIView beginAnimations:nil context:NULL];
    [UIViewsetAnimationDuration:0.2];
    wheel.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
}

EDIT: Actually, I would probably make the rotation (in the example -130 degrees) slightly more than what half would be, because CGAffineTransformIdentity is gonna take the shortest path to go back to regular, so if you go exactly 1/2 way, it may not go the correct direction (clockwise or counter-clockwise) that you want.

Related Topic