使用CGAffineTransformIdentity逆时针更改为顺时针方向

时间:2009-08-04 21:55:34

标签: iphone core-animation

我有一个电话轮。在touchend上,它通过动画转到他的位置。

直到角度小于180°,它顺时针返回。没问题,使用此代码:

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

但之后出现问题并继续旋转以进行完整的转换。

我试图制作这样的动画:

- (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];
}

它有效,但动画不流畅;变化是可见的。

1 个答案:

答案 0 :(得分:1)

我不确定touchesEnded(在旋转方面)状态是什么,我在假设你选择了DegreeToRadians(-130)尝试部分地完成它并期望下一个方法完成其余的工作。这应该是一个更好的解决方案,希望产生您期望的结果。我不确定卡侬是什么或为什么要旋转它,所以我只是转动轮子。

- (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];
}

编辑:实际上,我可能会使旋转(在示例中为-130度)稍微超过一半,因为CGAffineTransformIdentity将采用最短路径返回常规,因此,如果你正好走1/2路,那么它可能没有你想要的正确方向(顺时针或逆时针)。