【问题标题】:Rotating Image multiple times and stopping at desired degree?多次旋转图像并以所需的程度停止?
【发布时间】:2017-06-30 20:26:48
【问题描述】:

我尝试了各种方法,但无法弄清楚如何实现这一点。我在互联网上搜索了很多,但无法弄清楚。

我有一个 ImageView(一个旋转的轮子),我想将它旋转 360 度 10 次(这只是为了给用户快速转动轮子的感觉),然后我想要将其旋转特定值,例如 90 度(但可能会有所不同)。

这个动画结束后,我想把 ImageView 带回到初始位置。

我如何做到这一点? 提前致谢。

【问题讨论】:

  • 你如何旋转图像视图?
  • 我使用 CGAffineTransformMakeRotation 旋转。我也试过 [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]
  • 尝试在最后应用CGAffineTransformIdentity使其恢复到原始状态。
  • 谢谢,CGAffineTransformIdentity 帮助了。但我关心的是问题的另一部分。有什么想法吗?

标签: ios objective-c core-animation image-rotation


【解决方案1】:

好吧,我发现了如何做到这一点。 我用下面的方法360度旋转10次,然后旋转特定的度数。

-(void)rotate:(int)degreeToRotate
{
  CGFloat radians = (M_PI/180) * degreeToRotate;
  [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations: ^{
    //configuring to rotate 360 degree 10 times
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 10;

    [_scoreImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

    } completion:^(BOOL finished) {
        //rotate by particular degree
        [ UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        // -radians, to ensure clockwise rotation
        self.scoreImageView.transform = CGAffineTransformMakeRotation(-radians);

         }  completion:^(BOOL finished) {
                //method call to reset image original position 
                [self performSelector:@selector(resetPosition) withObject:nil afterDelay:3];
        }];

 }];

}

以下方法用于将图像重置为原始位置。

-(void)resetPosition
{
    [UIView animateWithDuration:0.2 animations:^() {

          self.scoreImageView.transform = CGAffineTransformIdentity;
    }]; 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多