Adding glow effect to UIButton - iOS

2019-01-21 20:38发布

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?

  Undefined symbols for architecture i386:
 "_OBJC_CLASS_$_CABasicAnimation", referenced from:
 objc-class-ref in UIView+Glow.o
 "_OBJC_CLASS_$_CAMediaTimingFunction", referenced from:
  objc-class-ref in UIView+Glow.o
  "_kCAMediaTimingFunctionEaseInEaseOut", referenced from:
  -[UIView(Glow) startGlowing] in UIView+Glow.o
   ld: symbol(s) not found for architecture i386
   clang: error: linker command failed with exit code 1 (use -v to see invocation)

5条回答
【Aperson】
2楼-- · 2019-01-21 20:47

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
查看更多
乱世女痞
3楼-- · 2019-01-21 20:49

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];
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-21 20:58

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

Example is available here

查看更多
疯言疯语
5楼-- · 2019-01-21 21:08

Swift 4

1- Create UIView extension with the animation

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

Note: u may change values and play around to get the effect u 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 = kCAFillModeRemoved
        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)
查看更多
你好瞎i
6楼-- · 2019-01-21 21:08

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

}
查看更多
登录 后发表回答