Flashing UIButton Animation

I was wondering if flashing animation can be applied to the UIB button. I searched, but only found code for pulsed animation, which continuously resized my UIB button. Instead, I thought of some kind of blinking animation to warn the user that he should press a button. The only approach to the problem that I can think of is to constantly change alpha using:

[self setAlpha:0.5]; 

... but it will not be visible as a flashing button.

+5
source share
5 answers

Perhaps this is not the best way and actually does not allow to stop blinking ... but it is simple, works and does not interfere with 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];
    }];
}
+16
source

, . .

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;
}];
+5

:

, /

blinkTimer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(toggleButtonImage:) userInfo:nil repeats:YES];

- (void)toggleButtonImage:(NSTimer*)timer
{

    if(toggle)
    {
        [blinkBtn setImage:[UIImage imageNamed:@"ButtonImage.png"] forState: UIControlStateNormal];
    }
    else
    {
        [blinkBtn setImage:[UIImage imageNamed:@"ButtonImage1.png"] forState: UIControlStateNormal];
    }
    toggle = !toggle;

}

.h

NSTimer *blinkTimer;
BOOL toggle;

, /

[blinkTimer invalidate];
+4

, .png [], , . , , , . - ?

+1

I did this by creating my own control, subclassing UIControl, as Apple does not recommend twisting the hierarchy of UIButton views. I add a background image representing the standard background image of the button and a β€œluminous” image above the background to represent the illuminated state and toggle its opacity to make it pulsate.

In addition, I can switch the opacity of the shadow of the layer so that it glows.

Initializing Code:

- (void)TS_commonButtonInit
{
    UIImage *shoutoutBackground            = [UIImage imageNamed:@"root-navigation-bar-share-button"];
    UIImage *shoutoutHighlightedBackground = [UIImage imageNamed:@"root-navigation-bar-share-button-highlighted"];
    UIImage *shoutoutPulseImage            = [UIImage imageNamed:@"root-navigation-bar-share-button-glowing"];

    shoutoutBackground            = [shoutoutBackground            stretchableImageWithLeftCapWidth:7 topCapHeight:0];
    shoutoutHighlightedBackground = [shoutoutHighlightedBackground stretchableImageWithLeftCapWidth:7 topCapHeight:0];
    shoutoutPulseImage            = [shoutoutPulseImage            stretchableImageWithLeftCapWidth:7 topCapHeight:0];

    [[self backgroundView] setImage:shoutoutBackground];
    [[self backgroundView] setHighlightedImage:shoutoutHighlightedBackground];

    [self setGlowingImage:shoutoutPulseImage];

    [self setExclusiveTouch:YES];

    [self addSubview:[self backgroundView]];
    [self addSubview:[self glowingImageView]];

    [[self layer] setShadowColor:[[UIColor colorWithHexString:@"ffc521" alpha:1] CGColor]];
    [[self layer] setShadowOpacity:0];
    [[self layer] setShadowRadius:5];
    [[self layer] setShadowOffset:CGSizeMake(0, 0)];
    [[self layer] setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:[self bounds] cornerRadius:6] CGPath]];
}

Impulse Code:

- (void)pulse:(NSInteger)numberOfTimes
{
    CGFloat pulseLength = .8;

    [[self glowingImageView] setAlpha:0];

    CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [pulseAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [pulseAnimation setDuration:pulseLength];
    [pulseAnimation setRepeatCount:numberOfTimes];
    [pulseAnimation setAutoreverses:YES];
    [pulseAnimation setFromValue:@(0)];
    [pulseAnimation setToValue:@(1)];
    [pulseAnimation setRemovedOnCompletion:YES];

    [[self layer] setShadowOpacity:0];

    CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
    [shadowAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [shadowAnimation setDuration:pulseLength];
    [shadowAnimation setRepeatCount:numberOfTimes];
    [shadowAnimation setAutoreverses:YES];
    [shadowAnimation setFromValue:@(0)];
    [shadowAnimation setToValue:@(1)];
    [shadowAnimation setRemovedOnCompletion:YES];

    [[[self glowingImageView] layer] addAnimation:pulseAnimation forKey:@"opacity"];
    [[self layer] addAnimation:shadowAnimation forKey:@"shadowOpacity"];
}
+1
source

All Articles