Iphone 使用CGAffineTransformity将逆时针更改为顺时针

Iphone 使用CGAffineTransformity将逆时针更改为顺时针,iphone,core-animation,Iphone,Core Animation,我有一个电话轮。在触摸端,它会以动画的形式移动到他的位置 直到角度小于180°,它将顺时针返回。没有问题,使用此代码: - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [UIView setAnimationBeginsFromCurrentState:YES]; [UIView beginAnimations:nil context:NULL]; [UIViewsetAnimationDuration:

我有一个电话轮。在触摸端,它会以动画的形式移动到他的位置

直到角度小于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)touchesend:(NSSet*)toucheevent:(UIEvent*)event{ [UIView setAnimationBeginsFromCurrentState:是]; [UIView beginAnimations:nil上下文:NULL]; [UIViewsetAnimationDuration:0.5]; wheel.transform=CGAffineTransformity; [UIView委员会]; } 但在那之后它就出了问题,继续旋转一个完整的回合

我尝试制作如下动画:

- (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]; } -(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{ [UIView setAnimationBeginsFromCurrentState:是]; [UIView beginAnimations:nil上下文:NULL]; [UIView设置动画持续时间:0.2]; wheel.transform=CGAffineTransformRotate(wheel.transform,度弧度(-130)); [UIView委员会]; [自执行选择器:@selector(animatewheelViewToCenter),对象:nil afterDelay:0.3]; } -(无效)动画WheelViewToCenter{ [UIView setAnimationBeginsFromCurrentState:是]; [UIView beginAnimations:nil上下文:NULL]; [UIViewsetAnimationDuration:0.3]; wheel.transform=CGAffineTransformity; [UIView委员会]; }
它可以工作,但动画不是流动的;变化是可见的。

我不确定触摸时的状态是什么(在旋转方面),我假设您选择了degreesToRadians(-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度)稍微超过一半,因为CGAffineTransformity将采用最短路径返回到常规,因此如果您正好走1/2的方向,它可能不会走正确的方向(顺时针或逆时针)你想要的。

我不确定触摸时的状态是什么(在旋转方面),我假设你选择了degreestorarians(-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度)稍微超过一半,因为CGAffineTransformity将采用最短路径返回到常规,因此如果您正好走1/2的方向,它可能不会走正确的方向(顺时针或逆时针)你想要的。

这是正确的,操作系统这样做的方式是错误的,动画会搜索最短路径以旋转回一致性,这就是为什么你要为响应终止problemThx。我编辑代码,将所有“cadran”更改为“wheel”。但是你的代码帮助了我,因为时间总是在动画和动画之间。然而,动画仍然不流畅。这是正确的,操作系统这样做的方式是错误的。为此,动画搜索最短路径以旋转回一致性,这就是为什么你要终止problemThx的响应。我编辑代码,将所有“cadran”更改为“wheel”。但是你的代码帮助了我,因为时间总是在动画和动画之间。但是,动画仍然不是流体。