UIButton flashing animation

后端 未结 5 2020
粉色の甜心
粉色の甜心 2021-02-03 15:20

I was wondering if it was possible to apply a flashing animation to a UIButton. I have searched but only found the code for a pulse animation which changes the size of my UIButt

5条回答
  •  醉梦人生
    2021-02-03 15:47

    Perhaps not the best way, and doesn't really allow you to stop the flashing... but this is simple, works, and does not hinder user interaction:

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

提交回复
热议问题