Iphone 如何连续旋转UIButton并使其停止?

Iphone 如何连续旋转UIButton并使其停止?,iphone,cocoa-touch,animation,Iphone,Cocoa Touch,Animation,我想连续旋转一个UIButton,作为一种指示应用程序正在录制内容的方法。这是个好主意吗。我也希望能够停止这种持续的旋转 MyButton.rotate360Degrees() 如何使用动画来完成此操作?使用UIView动画方法: animateWithDuration:delay:options:animations:completion: 并使用选项ui查看动画选项repeat 例如,对于名为按钮的ui按钮*出口: - (void)viewDidLoad { [super vie

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

MyButton.rotate360Degrees()

如何使用动画来完成此操作?

使用UIView动画方法:

animateWithDuration:delay:options:animations:completion:
并使用选项
ui查看动画选项repeat

例如,对于名为
按钮的
ui按钮*
出口:

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

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

我建议您不要旋转UIButton,只旋转按钮上的图像。谁知道头顶旋转一个能够检测触摸的矩形物体会带来什么复杂情况?如果用户不知何故轻触按钮的角落,然后在按钮旋转时释放轻触,从而使触摸现在处于外部,会发生什么情况?那里面有没有补漆

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

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
这似乎很合适。完整旋转(2pi)将每隔
持续时间发生一次,您将使其重复,直到使用
UIViewAnimationOptionRepeat
停止为止。您可以使用
completion
进行检查,查看是否应在一个动画周期结束时开始另一个周期,并且可以随时使用停止动画

[myView.layer removeAllAnimations];

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

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

#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()
停下来

MyButton.layer.removeAllAnimations()

请记住,iOS4及更高版本不鼓励使用
beginAnimation
方法,建议使用基于块的动画。为什么要运行自己的动画循环呢?另外,您还需要添加QuartzCore框架,并导入不执行半个回合的动画循环,而是执行整个回合的动画循环。(360*M_-PI)/180==2*M_-PI,因为角度以弧度表示,实际上是一个完整的旋转。@Abizern我使用的是带有“UITableViewCell”背景色的这种动画。当细胞离开屏幕并返回时,我会释放动画。但是,当单元返回时,使用这种方法再次启动动画不起作用。你知道为什么吗?(我在“cellForRowAtIndexPath”委托方法中再次开始)如果您不关心旋转方向,那么这种方式很好。animateWithDuration将选择最短路径以实现所需的旋转效果,因此无法保证按钮将朝哪个方向旋转。如果要控制方向,请使用CABasicAnimation或CAKeyFrameAnimation(如下面所述的Nikunj)。但是,这将停止的用户交互button@EshwarChaitanya如果要在动画块期间允许用户交互,请将
UIViewAnimationOptionAllowUserInteraction
添加到选项中。@Abizem还有一个问题,如何仅为标题忽略动画,我尝试了这一行[button.titleLabel.layer removeAnimationForKey:@“SpinAnimation”];…但不起作用