如何在iPhone中以360度编程方式旋转按钮?

时间:2013-03-23 10:11:39

标签: iphone objective-c cocoa-touch

如何将按钮旋转360度持续30秒,之后按钮停止旋转。

4 个答案:

答案 0 :(得分:8)

360度旋转动画只是使用Core Animation的几行代码。

CABasicAnimation *rotate = 
    [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.byValue = @(M_PI*2); // Change to - angle for counter clockwise rotation
rotate.duration = 30.0; 

[yourButton.layer addAnimation:rotate 
                        forKey:@"myRotationAnimation"];

通过使用byValue属性,您正在对之前的任何旋转进行360度的相对旋转(与显式指定from和to值相比)。这意味着上面的代码将按钮旋转360度,即使它已经旋转。显式指定结束变换的所有答案都假设按钮尚未旋转。

上面的例子尽可能地小到你所要求的(“旋转360度,持续时间30秒”)。如果您想要更多控制,可以选择通过指定计时功能

来使动画开始和/或缓慢停止
rotate.timingFunction = 
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 

如果您尚未将QuarzCore.framework添加到项目中,则需要执行此操作。此外,#import <QuartzCore/QuartzCore.h>位于源文件的顶部。

答案 1 :(得分:0)

CATransform3D myRotationTransform = CATransform3DRotate(Yourbutton.layer.transform, -1, 0.0, 0.0, 1.0);
Yourbutton.layer.transform = myRotationTransform;

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: -1];
animation.duration = 30;
animation.repeatCount = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[Yourbutton.layer addAnimation:animation forKey:@"MyAnimation"];

应该根据需要工作!别忘了包括quartz.framework!

答案 2 :(得分:0)

我刚刚使用

self.buttonImage查看你需要旋转的ImageView。

[UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{

    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
        self.buttonImageView.transform= CGAffineTransformMakeRotation(M_PI);
    }];

    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.25 animations:^{
        self.buttonImageView.transform= CGAffineTransformMakeRotation(-2* M_PI);
    }];


} completion:^(BOOL finished) {

    [self.map setCenterCoordinate:self.map.userLocation.location.coordinate animated:YES];
    self.buttonImageView.transform= CGAffineTransformMakeRotation(2* M_PI);
}];

答案 3 :(得分:-1)

[UIView animateWithDuration:.30f animations:^{
    btnGallery.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI);
}];