为UIButton添加发光效果 – iOS

我有一个UIButton这是一个标志。 这个标志button将永远发光,但会停止发光touch.It就像一个发光的animation。

有什么build议吗?

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) 

我build议你使用秘密实验室制作的UIView的发光类别 。

例子可以在这里find

我喜欢这个发光+增长/缩小animation为我的“特别”button:

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

发光的代码取自: 为UILabel和UIButton创build一个发光效果

首先,你需要导入QuartzCore框架:

 #import <QuartzCore/QuartzCore.h> 

当你创build一个button(或在viewDidLoad ,取决于你的代码结构)添加此代码:

 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; 

你需要注意两个事件: UIControlEventTouchDownUIControlEventTouchUpInside

UIControlEventTouchDown处理程序中,您将添加代码:

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

UIControlEventUpInside处理程序中,您将添加代码:

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

实施的细节取决于你是否创buildbutton程序或通过Interface Builder,但我相信你能从这里弄清楚这一点。

编辑:只需添加下面的代码自定义button应该工作:

 [button setImage:[UIImage imageNamed:@"buttonWithGlow.png"] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"buttonWithNoGlow.png"] forState:UIControlStateHighlighted]; 

这是我的回答….

使用类别

 /* ****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