为UIButton添加发光效果 - iOS

Sid*_*kan 18 xcode quartz-graphics ios

我有一个UIButton,这是一个标志.这个徽标按钮将永远发光,但触摸时会停止发光.它就像一个发光的动画.

有什么建议吗?

  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)
Run Code Online (Sandbox Code Playgroud)

YaD*_*aDa 25

我喜欢这个发光+增长/缩小动画的"特殊"按钮:

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

}
Run Code Online (Sandbox Code Playgroud)


Rok*_*arc 8

发光代码取自:为UILabel和UIButton创建发光效果

首先,您需要导入QuartzCore Framework:

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)

当您创建一个按钮(或在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;
Run Code Online (Sandbox Code Playgroud)

你需要注意两个事件:UIControlEventTouchDownUIControlEventTouchUpInside

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

UIColor *color = [UIColor clearColor];
button.titleLabel.layer.shadowColor = [color CGColor];
Run Code Online (Sandbox Code Playgroud)

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

UIColor *color = button.currentTitleColor;
button.titleLabel.layer.shadowColor = [color CGColor];
Run Code Online (Sandbox Code Playgroud)

再次实现的细节取决于你是创建按钮programmaticaly还是通过Interface Builder,但我相信你将能够从这里开始解决这个问题.

编辑:对于自定义按钮,只需添加以下代码应该工作:

[button setImage:[UIImage imageNamed:@"buttonWithGlow.png"]
        forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"buttonWithNoGlow.png"] 
        forState:UIControlStateHighlighted];
Run Code Online (Sandbox Code Playgroud)


ale*_*los 8

迅捷5

  1. 使用动画创建UIView扩展

  2. 在文本字段,按钮,视图(UIView的任何子类)上使用它

注意:您可以更改值并四处移动以获得所需的效果。

UIView扩展

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")
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用它:

//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)
Run Code Online (Sandbox Code Playgroud)