UIButton flashing animation

后端 未结 5 2032
粉色の甜心
粉色の甜心 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:31

    I did this by creating my own control, subclassing UIControl since Apple doesn't recommend screwing with the view hierarchy of UIButton. I add a background imageView representing the standard background image of the button, and a "glowing" imageView above the background to represent the lit-up state, and toggle its opacity to make it pulse.

    I additionally toggle the layer's shadow opacity to make it glow.

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

    Pulsing 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"];
    }
    

提交回复
热议问题