繁体   English   中英

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

[英]How to programmatically rotate button by 360 Degrees in iPhone?

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

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>

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!

好吧,我刚刚用过

作为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);
}];
[UIView animateWithDuration:.30f animations:^{
    btnGallery.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI);
}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM