如何使用单个按钮在不同方向旋转图像?

时间:2014-08-08 11:07:38

标签: ios objective-c xcode

我在添加案例开关的代码时遇到了问题,或者只用一个按钮就可以了,这样当我按下它时它可以旋转图像,而当我再次点击它时,它应该在其他方向旋转。

1 个答案:

答案 0 :(得分:0)

试试这个,

此处yourImage是要旋转的图像视图

- (IBAction)rotateImage:(UIButton *)sender // your button action method
{
    if (!sender.selected)
    {
        [sender setSelected:YES];
        [UIView animateKeyframesWithDuration:2.0
                                       delay:0.0
                                     options:UIViewKeyframeAnimationOptionCalculationModeLinear
                                  animations:^{
                                      [UIView addKeyframeWithRelativeStartTime:0.0
                                                              relativeDuration:1/3.0
                                                                    animations:^{
                                                                        yourImage.transform = CGAffineTransformMakeRotation(2.0 * M_PI / 3.0);
                                                                    }];
                                      [UIView addKeyframeWithRelativeStartTime:1/3.0
                                                              relativeDuration:1/3.0
                                                                    animations:^{
                                                                        yourImage.transform = CGAffineTransformMakeRotation(4.0 * M_PI / 3.0);
                                                                    }];
                                      [UIView addKeyframeWithRelativeStartTime:2/3.0
                                                              relativeDuration:1/3.0
                                                                    animations:^{
                                                                        yourImage.transform = CGAffineTransformMakeRotation(0);
                                                                    }];

                                  }
                                  completion:^(BOOL finished) {

                                  }];
        }
    else
    {
        [sender setSelected:NO];

        [UIView animateKeyframesWithDuration:2.0
                                       delay:0.0
                                     options:UIViewKeyframeAnimationOptionCalculationModeLinear
                                  animations:^{
                                      [UIView addKeyframeWithRelativeStartTime:0.0
                                                              relativeDuration:1/3.0
                                                                    animations:^{
                                                                        yourImage.transform = CGAffineTransformMakeRotation(4.0 * M_PI / 3.0);
                                                                    }];
                                      [UIView addKeyframeWithRelativeStartTime:1/3.0
                                                              relativeDuration:1/3.0
                                                                    animations:^{
                                                                        yourImage.transform = CGAffineTransformMakeRotation(2.0 * M_PI / 3.0);
                                                                    }];
                                      [UIView addKeyframeWithRelativeStartTime:2/3.0
                                                              relativeDuration:1/3.0
                                                                    animations:^{
                                                                        yourImage.transform = CGAffineTransformMakeRotation(0);
                                                                    }];

                                  }
                                  completion:^(BOOL finished) {

                                  }];
    }
}