【问题标题】:how to rotate an image in different direction using single button?如何使用单个按钮在不同方向旋转图像?
【发布时间】:2014-08-08 11:07:38
【问题描述】:

我在添加大小写切换代码时遇到问题,或者如果使用单个按钮,它可以在我单击它时旋转图像,而不是当我再次点击它时,它应该向其他方向旋转.

【问题讨论】:

  • 你能告诉我们你尝试了什么吗?

标签: ios objective-c xcode


【解决方案1】:

试试这个,

这里 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) {

                                  }];
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多