2

如何在 30 秒的时间内将按钮旋转 360 度,然后按钮停止旋转。

4

4 回答 4

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 度。所有明确指定结束变换的答案都假设按钮尚未旋转。

上面的示例尽可能小,以按照您的要求进行(“在 30 秒的时间内旋转 360 度”)。如果您想拥有更多控制权,您可以选择通过指定计时函数来使动画缓慢开始和/或停止

rotate.timingFunction = 
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 

如果您还没有添加QuarzCore.framework到您的项目中,您将需要这样做。也在#import <QuartzCore/QuartzCore.h>源文件的顶部。

于 2013-03-23T10:50:58.410 回答
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!

于 2013-03-23T10:48:42.110 回答
0

嗯,我刚用

作为 self.buttonImageView 您需要旋转的 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);
}];
于 2016-05-04T03:51:38.720 回答
-1
[UIView animateWithDuration:.30f animations:^{
    btnGallery.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI);
}];
于 2013-03-23T10:16:18.783 回答