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

时间:2011-09-07 05:53:34

标签: iphone cocoa-touch animation

我想连续旋转UIButton,以表明应用正在录制某些内容。这是一个好主意吗。我也希望能够阻止这种持续的轮换。

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

6 个答案:

答案 0 :(得分:35)

使用UIView动画方法:

animateWithDuration:delay:options:animations:completion:

并使用选项UIViewAnimationOptionRepeat

例如,对于名为UIButton *的{​​{1}}商店:

button

由于它是圆形的,我只是用pi弧度而不是2pi旋转它。你可以使用你想要的任何角度。

修改

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

- (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];
}

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

答案 1 :(得分:6)

尝试以下可以帮助您的代码,因为我成功运行了该代码。

    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"];

答案 2 :(得分:3)

我自己也有同样的问题。最后我这样做了:

#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];
                 }];
}

答案 3 :(得分:2)

尝试以下扩展名为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()

对于停止。

MyButton.layer.removeAllAnimations()

答案 4 :(得分:0)

我建议您不要旋转UIButton,只是旋转按钮上的图像。谁知道旋转可以检测到触摸的矩形物体会引起什么样的复杂性或开销 - 如果您的用户以某种方式轻敲按钮的角落然后在按钮旋转时释放水龙头以便触摸现在在外面会发生什么?是否注册了内部补漆?

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

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

似乎符合要求。每duration次完成一次旋转(2pi)并使其重复,直到您使用UIViewAnimationOptionRepeat停止它。您可以查看completion以查看是否应该在一个动画周期结束后开始另一个周期,并且可以随时停止动画

[myView.layer removeAllAnimations];

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

答案 5 :(得分:-2)

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDelegate:self];

[UIView setAnimationDuration:0.3];

uiButtonObject.transform = CGAffineTransformMakeRotation(M_PI);

[UIView commitAnimations];

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