Adding glow effect to UIButton - iOS

后端 未结 6 1916
一生所求
一生所求 2020-12-04 22:25

I have an UIButton which is a logo. This logo button will glow on forever but will stop glowing on touch.It is like a glowing animation.

Is there any suggestions?

相关标签:
6条回答
  • 2020-12-04 23:08

    I suggest you use the Glow Category of UIView made by secret lab.

    Example is available here

    0 讨论(0)
  • 2020-12-04 23:08

    Swift 5

    alegelos answer with shadowOpacity, autorepeat and shadowOffset:

    extension UIView {
    
      enum GlowEffect: Float {
        case small = 0.4, normal = 5, big = 15
      }
      
      func doGlowAnimation(withColor color: UIColor, withEffect effect: GlowEffect = .normal) {
        layer.masksToBounds = false
        layer.shadowColor = color.cgColor
        layer.shadowRadius = 0
        layer.shadowOpacity = 1
        layer.shadowOffset = CGSize(width: 0, height: 3)
        
        let glowAnimationRadius = CABasicAnimation(keyPath: "shadowRadius")
        glowAnimationRadius.fromValue = 0
        glowAnimationRadius.toValue = effect.rawValue
        glowAnimationRadius.beginTime = CACurrentMediaTime()+0.3
        glowAnimationRadius.duration = CFTimeInterval(1.3)
        glowAnimationRadius.fillMode = .removed
        glowAnimationRadius.autoreverses = true
        glowAnimationRadius.repeatCount = .infinity
        layer.add(glowAnimationRadius, forKey: "shadowGlowingAnimationRadius")
        
        let glowAnimationOpacity = CABasicAnimation(keyPath: "shadowOpacity")
        glowAnimationOpacity.fromValue = 0
        glowAnimationOpacity.toValue = 1
        glowAnimationOpacity.beginTime = CACurrentMediaTime()+0.3
        glowAnimationOpacity.duration = CFTimeInterval(1.3)
        glowAnimationOpacity.fillMode = .removed
        glowAnimationOpacity.autoreverses = true
        glowAnimationOpacity.repeatCount = .infinity
        layer.add(glowAnimationOpacity, forKey: "shadowGlowingAnimationOpacity")
      }
    }
    
    0 讨论(0)
  • 2020-12-04 23:17

    I like this glow + grow/shrink animation for my 'extra special' buttons:

    -(void)makeViewShine:(UIView*) view
    {
    view.layer.shadowColor = [UIColor yellowColor].CGColor;
    view.layer.shadowRadius = 10.0f;
    view.layer.shadowOpacity = 1.0f;
    view.layer.shadowOffset = CGSizeZero;
    
    
    [UIView animateWithDuration:0.7f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction  animations:^{
    
        [UIView setAnimationRepeatCount:15];
    
        view.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
    
    
    } completion:^(BOOL finished) {
    
        view.layer.shadowRadius = 0.0f;
        view.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
    }];
    
    }
    
    0 讨论(0)
  • 2020-12-04 23:17

    Glowing code taken from: Creating a Glow Effect for UILabel and UIButton

    First, you'll need to import the QuartzCore Framework:

    #import <QuartzCore/QuartzCore.h>
    

    When you create a button (or in viewDidLoad, depends on your code structure) add this code:

    UIColor *color = button.currentTitleColor;
    button.titleLabel.layer.shadowColor = [color CGColor];
    button.titleLabel.layer.shadowRadius = 4.0f;
    button.titleLabel.layer.shadowOpacity = .9;
    button.titleLabel.layer.shadowOffset = CGSizeZero;
    button.titleLabel.layer.masksToBounds = NO;
    

    You'll need to watch for two events: UIControlEventTouchDown and UIControlEventTouchUpInside

    In UIControlEventTouchDown handler you'll add the code:

    UIColor *color = [UIColor clearColor];
    button.titleLabel.layer.shadowColor = [color CGColor];
    

    And in UIControlEventUpInside handler you'll add the code:

    UIColor *color = button.currentTitleColor;
    button.titleLabel.layer.shadowColor = [color CGColor];
    

    Again details of implementation depend on whether you create button programmaticaly or via Interface Builder but i'm sure you'll be able to figure this out from here on.

    EDIT: for a custom button simply adding the following code should work:

    [button setImage:[UIImage imageNamed:@"buttonWithGlow.png"]
            forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"buttonWithNoGlow.png"] 
            forState:UIControlStateHighlighted];
    
    0 讨论(0)
  • 2020-12-04 23:18

    Swift 5

    1. Create UIView extension with the animation

    2. Use it on textfields, buttons, views (any subclass of UIView)

    Note: you may change values and play around to get the effect you need.

    UIView extension

    import UIKit
    
    extension UIView {
        enum GlowEffect: Float {
            case small = 0.4, normal = 2, big = 15
        }
    
        func doGlowAnimation(withColor color: UIColor, withEffect effect: GlowEffect = .normal) {
            layer.masksToBounds = false
            layer.shadowColor = color.cgColor
            layer.shadowRadius = 0
            layer.shadowOpacity = 1
            layer.shadowOffset = .zero
    
            let glowAnimation = CABasicAnimation(keyPath: "shadowRadius")
            glowAnimation.fromValue = 0
            glowAnimation.toValue = effect.rawValue
            glowAnimation.beginTime = CACurrentMediaTime()+0.3
            glowAnimation.duration = CFTimeInterval(0.3)
            glowAnimation.fillMode = .removed
            glowAnimation.autoreverses = true
            glowAnimation.isRemovedOnCompletion = true
            layer.add(glowAnimation, forKey: "shadowGlowingAnimation")
        }
    }
    

    How to use it:

    //TextField with border
    textField.doGlowAnimation(withColor: UIColor.red, withEffect: .big)
    
    //Label
    label.doGlowAnimation(withColor: label.textColor, withEffect: .small)
    
    //Button
    button.doGlowAnimation(withColor: UIColor.red, withEffect: .big)
    
    0 讨论(0)
  • 2020-12-04 23:19

    Here is my answer....

    Using categories

    /* ****FAQ?
    
     1.how to add glow effect on uibutton?
     [UIButton glowUIButton:playButton];
    
     2.how to remove effect on uibutton?
     [UIButton removeGlowUIButton:playButton];
    
     Ends*** */
    
    #import <UIKit/UIKit.h>
    @interface UIButton (BlinkEffect)
    //blink effect category
    +( void) glowUIButton:(UIButton *)inputButton;
    //remove blink effect
    +(void) removeGlowUIButton:(UIButton *)inputButton;
    @end
    
    #import "UIButton+BlinkEffect.h"
    
    @implementation UIButton (BlinkEffect)
    
    +(void) glowUIButton:(UIButton *)inputButton
    {
        //add blink effect
        CALayer *viewLayer = inputButton.layer;
        viewLayer.shadowOffset = CGSizeMake(0,0);
        CGFloat radius = CGRectGetWidth(inputButton.bounds)/2.0;
        viewLayer.shadowColor = [[UIColor whiteColor] CGColor];
        viewLayer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-7,-5.5, 2.5 * (radius), 2.5 * radius) cornerRadius:radius].CGPath;
        viewLayer.shadowRadius = 5.0f;
        viewLayer.shadowOpacity = 1.0f;
    
        //Let's animate it while we're at it.
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
        animation.duration =0.7;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.fromValue = [NSNumber numberWithFloat:1.0];
        animation.toValue = [NSNumber numberWithFloat:0.0];
        animation.autoreverses = YES;
        animation.repeatCount = 1.0 / 0.0;
        [viewLayer addAnimation:animation forKey:@"shadowOpacity"];
    
    }
    
    +(void)removeGlowUIButton:(UIButton *)inputButton
    {
        CALayer *viewLayer = inputButton.layer;
        viewLayer.shadowColor = [[UIColor clearColor] CGColor];
        [viewLayer removeAnimationForKey:@"shadowOpacity"];
    }
    @end
    
    0 讨论(0)
提交回复
热议问题