如何连续旋转UIButton并能够停止它



我想连续旋转UIButton,作为指示应用程序正在录制内容的一种方式。这是个好主意吗。我也希望能够停止这种连续的旋转。

我该如何使用动画来做到这一点?

使用UIView动画方法:

animateWithDuration:delay:options:animations:completion:

并使用选项UIViewAnimationOptionRepeat

例如,对于名为button:的UIButton *出口

- (void)viewDidLoad
{
    [super viewDidLoad];
    [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations:^{
        CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
        self.button.transform = transform;
    } completion:NULL];
}

因为它是一圈又一圈的,我只是把它旋转π弧度,而不是2pi。你可以使用任何你想要的角度。

编辑

要停止动画,只需从当前状态开始创建另一个短持续时间的动画,例如

- (void)stopRotating {
    [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear animations:^{
        CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 0.05);
        self.button.transform = transform;
    } completion:NULL];
}

这将提供一个非常短的动画,覆盖当前动画。注意,变换的角度乘以0.05,0.05是0.1/2.0的比率,这意味着这个小片段的旋转速度与连续旋转速度相同。

请尝试下面的代码,因为我成功地运行了这些代码,所以这些代码将对您有所帮助。

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];
    CABasicAnimation *halfTurn;
    halfTurn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    halfTurn.fromValue = [NSNumber numberWithFloat:0];
    halfTurn.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    halfTurn.duration = 0.5;
    halfTurn.repeatCount = HUGE_VALF;
    [[button layer] addAnimation:halfTurn forKey:@"180"];

我自己也遇到了同样的问题。最后我做到了:

#pragma mark Start/StopSpining
-(void)startSpin{
   isSpining = YES;
   [self Spin];
}
-(void)stopSpin{
   isSpining = NO;
}
-(void)spin{
[UIView animateWithDuration:1.0 delay:0 options: UIViewAnimationOptionCurveLinear animations:^{
   self.spinCircle.transform = CGAffineTransformRotate(self.spinCircle.transform, M_PI/2);
}  completion:^(BOOL finished) {
                if (isSpining)
                     [self spin];
                 }];
}

请尝试以下swift 3的扩展。

extension UIView {
    func rotate360Degrees(duration: CFTimeInterval = 3) {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = CGFloat(M_PI * 2)
        rotateAnimation.isRemovedOnCompletion = false
        rotateAnimation.duration = duration
        rotateAnimation.repeatCount=Float.infinity
        self.layer.add(rotateAnimation, forKey: nil)
    }
}

用于开始旋转。

MyButton.rotate360Degrees()

还有Stop。

MyButton.layer.removeAllAnimations()

我建议你不要旋转UIButton,只旋转你放在按钮上的图像。谁知道旋转一个可以检测触摸的矩形物体会带来什么复杂情况?如果你的用户以某种方式点击按钮的角落,然后在按钮旋转时释放点击,从而使触摸处于外部,会发生什么?那里面有没有补漆?

无论如何,如果你想连续旋转,请使用块方法:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

似乎符合要求。完整的旋转(2pi(将在每个duration发生,并且您使其重复,直到您使用UIViewAnimationOptionRepeat停止为止。您可以使用completion检查是否应该在一个动画循环结束后开始另一个循环,并且您可以随时使用停止动画

[myView.layer removeAllAnimations];

(如果你只是想运行它,直到你停止它,那么这个方法还有其他变体,没有完成块(

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
uiButtonObject.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];

M_PI是要旋转的角度的弧度值。在循环和其他线程中运行此操作。您可能还想使用performSelectInMainThread方法运行上述操作,因为UIThread中不允许更改UI。

最新更新