如何连续旋转UIButton并能停止它? [英] How can I rotate a UIButton continuously and be able to stop it?

查看:112
本文介绍了如何连续旋转UIButton并能停止它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连续旋转UIButton来表示应用程式正在录音。这是一个好主意。



我如何使用动画来做这个?

解决方案

使用UIView动画方法:

  animateWithDuration:delay:options:animations:completion :

并使用选项 UIViewAnimationOptionRepeat p>

例如对于 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.1 / 2.0的比率,这意味着该小段的旋转速度与连续旋转速度相同。


I want to rotate a UIButton continuously as a means of indicating that an app is recording something. Is this a good idea. I also want to be able to stop this continuous rotation.

How would I use animation to do this?

解决方案

Use the UIView animation method:

animateWithDuration:delay:options:animations:completion:

and use the option UIViewAnimationOptionRepeat

For example for a UIButton * outlet named button:

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

Since it's going round and round, I just rotate it by pi radians rather than 2pi. you could use any angle you want.

Edit

To stop the animation, just create another animation of a short duration beginning from the current state, e.g.

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

This provides a very short animation which overrides the current animation. Note that the angle of the transform is multiplied by 0.05 which is the ratio of 0.1/2.0 which means that the rotational speed for this little segment is the same as the continuous rotational speed.

这篇关于如何连续旋转UIButton并能停止它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆