UI按钮闪烁动画

Ale*_*dro 5 animation uikit ios

我想知道是否可以将闪烁动画应用于UI按钮.我在互联网上搜索过,但只找到了脉冲动画的代码,它不断改变我的Uibutton的大小.我改为考虑某种闪烁动画来提醒用户他必须按下按钮.我能想到的问题的唯一方法是使用以下方法不断更改alpha:

[self setAlpha:0.5]; 
Run Code Online (Sandbox Code Playgroud)

...但它不会像闪烁按钮那样可见.

Mik*_*keS 16

也许不是最好的方式,并不能真正让你停止闪烁...但这很简单,有效,并且不会妨碍用户交互:

- (void)viewDidLoad
{
    [self flashOn:myButton];
}

- (void)flashOff:(UIView *)v
{
    [UIView animateWithDuration:.05 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
        v.alpha = .01;  //don't animate alpha to 0, otherwise you won't be able to interact with it
    } completion:^(BOOL finished) {
        [self flashOn:v];
    }];
}

- (void)flashOn:(UIView *)v
{
    [UIView animateWithDuration:.05 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
        v.alpha = 1;
    } completion:^(BOOL finished) {
        [self flashOff:v];
    }];
}
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用`UIViewAnimationOptionAutoreverse`动画选项来简化代码.此外,如果您不想永久循环动画,请从动画块内部调用`setAnimationRepeatCount:`. (4认同)

Enc*_*ira 5

到目前为止阅读所有答案我找到了以下解决方案.它重复了很多次并为我完成了这项工作.

CGFloat oldAlpha = v.alpha;
CGFloat minAlpha = 0.2;
enum UIViewAnimationOptions options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut;

[UIView animateWithDuration:.3 delay:0.3 options:options animations:^{
    [UIView setAnimationRepeatCount:4];
    v.alpha = minAlpha;
}
completion:^(BOOL finished) {
    v.alpha = oldAlpha;
}];
Run Code Online (Sandbox Code Playgroud)