带辉光的文字和图片效果

效果如下:



本人已经将实现封装到一个分类里面,可以直接调用:

    // label
    UILabel *glowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
    glowLabel.center = self.view.center;
    glowLabel.text = @"LeiLuRong...";
    glowLabel.textAlignment = NSTextAlignmentCenter;
    glowLabel.textColor = [UIColor whiteColor];
    glowLabel.font = [UIFont systemFontOfSize:28];
    [self.view addSubview:glowLabel];
    
    glowLabel.glowRadius = @(2.f);
    glowLabel.glowOpacity = @(1.f);
    glowLabel.glowColor = [UIColor cyanColor];
    
    glowLabel.glowDuration = @(1.f);
    glowLabel.hideDuration = @(0.3);
    glowLabel.glowAnimationDuration = @(1.5f);
    
    [glowLabel createGlowLayer];
    [glowLabel insertGlowLayer];
    [glowLabel startGlowLoop];
    
    
    
    // imageView
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    imageView.center = CGPointMake(self.view.center.x, 400);
    imageView.image = [UIImage imageNamed:@"collection_new"];
    [self.view addSubview:imageView];
    
    imageView.glowRadius = @(2.f);
    imageView.glowOpacity = @(0.5f);
    imageView.glowColor = [UIColor whiteColor];
    
    imageView.glowDuration = @1.5;
    imageView.hideDuration = @0.5;
    imageView.glowAnimationDuration = @1.5;
    
    [imageView createGlowLayer];
    [imageView insertGlowLayer];
    [imageView startGlowLoop];

分类:
UIView+GlowView.h

/**
 *  辉光的颜色
 */
@property (nonatomic, strong) UIColor  *glowColor;

/**
 *  辉光的透明度
 */
@property (nonatomic, strong) NSNumber *glowOpacity;

/**
 *  辉光的阴影半径
 */
@property (nonatomic, strong) NSNumber *glowRadius;

#pragma mark - 设置辉光时间间隔

/**
 *  一次完整的辉光周期(从显示到透明或者从透明到显示),默认1s
 */
@property (nonatomic, strong) NSNumber *glowAnimationDuration;

/**
 *  保持辉光时间(不设置,默认为0.5s)
 */
@property (nonatomic, strong) NSNumber *glowDuration;

/**
 *  不显示辉光的周期(不设置默认为0.5s)
 */
@property (nonatomic, strong) NSNumber *hideDuration;

#pragma mark - 辉光相关操作

/**
 *  创建出辉光layer
 */
- (void)createGlowLayer;

/**
 *  插入辉光的layer
 */
- (void)insertGlowLayer;

/**
 *  移除辉光的layer
 */
- (void)removeGlowLayer;

/**
 *  显示辉光
 */
- (void)glowToshowAnimated:(BOOL)animated;

/**
 *  隐藏辉光
 */
- (void)glowToHideAnimated:(BOOL)animated;

/**
 *  开始循环辉光
 */
- (void)startGlowLoop;

UIView+GlowView.m

#import "UIView+GlowView.h"
#import <objc/runtime.h>

@interface UIView ()

@property (nonatomic, strong) CALayer           *glowLayer;
@property (nonatomic, strong) dispatch_source_t  dispatchSource;

@end

@implementation UIView (GlowView)

- (void)createGlowLayer {
    
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIBezierPath* path = [UIBezierPath bezierPathWithRect:self.bounds];
    [[self accessGlowColor] setFill];
    [path fillWithBlendMode:kCGBlendModeSourceAtop alpha:1.0];
    
    self.glowLayer = [CALayer layer];
    self.glowLayer.frame = self.bounds;
    self.glowLayer.contents = (__bridge id)UIGraphicsGetImageFromCurrentImageContext().CGImage;
    self.glowLayer.opacity = 0.f;
    self.glowLayer.shadowOffset = CGSizeMake(0, 0);
    self.glowLayer.shadowOpacity = 1.f;
    
    UIGraphicsEndImageContext();
}

- (void)insertGlowLayer {
    
    if (self.glowLayer) {
        
        [self.layer addSublayer:self.glowLayer];
    }
}

- (void)removeGlowLayer {
    
    if (self.glowLayer) {
        
        [self.glowLayer removeFromSuperlayer];
    }
}

- (void)glowToshowAnimated:(BOOL)animated {
    
    self.glowLayer.shadowColor = [self accessGlowColor].CGColor;
    self.glowLayer.shadowRadius = [self accessGlowRadius].floatValue;
    
    if (animated) {
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation.fromValue = @(0.f);
        animation.toValue = [self accessGlowOpacity];
        self.glowLayer.opacity = [self accessGlowOpacity].floatValue;
        animation.duration = [self accessAnimationDuration].floatValue;
        
        [self.glowLayer addAnimation:animation forKey:@"glowLayerOpacity"];
        
    } else {
        
        [self.glowLayer removeAnimationForKey:@"glowLayerOpacity"];
        self.glowLayer.opacity = [self accessGlowOpacity].floatValue;
    }
}

- (void)glowToHideAnimated:(BOOL)animated {
    
    self.glowLayer.shadowColor = [self accessGlowColor].CGColor;
    self.glowLayer.shadowRadius = [self accessGlowRadius].floatValue;
    
    if (animated) {
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation.fromValue = [self accessGlowOpacity];
        animation.toValue = @(0.f);
        self.glowLayer.opacity = 0.f;
        animation.duration = [self accessAnimationDuration].floatValue;
        
        [self.glowLayer addAnimation:animation forKey:@"glowLayerOpacity"];
        
    } else {
        
        [self.glowLayer removeAnimationForKey:@"glowLayerOpacity"];
        self.glowLayer.opacity = 0.f;
    }
}

- (void)startGlowLoop {
    
    if (self.dispatchSource == nil) {
        
        CGFloat seconds = [self accessAnimationDuration].floatValue * 2 + [self accessGlowDuration].floatValue + [self accessHideDuration].floatValue;
        CGFloat delaySeconds = [self accessAnimationDuration].floatValue + [self accessGlowDuration].floatValue;
        
        self.dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
        
        __weak UIView *weakSelf = self;
        dispatch_source_set_timer(self.dispatchSource, dispatch_time(DISPATCH_TIME_NOW, 0), NSEC_PER_SEC * seconds, 0);
        dispatch_source_set_event_handler(self.dispatchSource, ^{
            
            [weakSelf glowToshowAnimated:YES];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * delaySeconds), dispatch_get_main_queue(), ^{
                
                [weakSelf glowToHideAnimated:YES];
            });
        });
        
        dispatch_resume(self.dispatchSource);
    }
}

#pragma mark - 处理数据越界问题

- (NSNumber *)accessGlowOpacity {
    
    if (self.glowOpacity) {
        
        if (self.glowOpacity.floatValue <= 0 || self.glowOpacity.floatValue > 1) {
            
            return @(0.8);
            
        } else {
            
            return self.glowOpacity;
        }
        
    } else {
        
        return @(0.8);
    }
}

- (NSNumber *)accessGlowDuration {
    
    if (self.glowDuration) {
        
        if (self.glowDuration.floatValue <= 0) {
            
            return @(0.5f);
            
        } else {
            
            return self.glowDuration;
        }
        
    } else {
        
        return @(0.5f);
    }
}

- (NSNumber *)accessHideDuration {
    
    if (self.hideDuration) {
        
        if (self.hideDuration.floatValue < 0) {
            
            return @(0.5);
            
        } else {
            
            return self.hideDuration;
        }
        
    } else {
        
        return @(0.5f);
    }
}

- (NSNumber *)accessAnimationDuration {
    
    if (self.glowAnimationDuration) {
        
        if (self.glowAnimationDuration.floatValue <= 0) {
            
            return @(1.f);
            
        } else {
            
            return self.glowAnimationDuration;
        }
        
    } else {
        
        return @(1.f);
    }
}

- (UIColor *)accessGlowColor {
    
    if (self.glowColor) {
        
        return self.glowColor;
        
    } else {
        
        return [UIColor redColor];
    }
}

- (NSNumber *)accessGlowRadius {
    
    if (self.glowRadius) {
        
        if (self.glowRadius.floatValue <= 0) {
            
            return @(2.f);
            
        } else {
            
            return self.glowRadius;
        }
        
    } else {
        
        return @(2.f);
    }
}

#pragma mark - runtime属性

- (void)setDispatchSource:(dispatch_source_t)dispatchSource {
    
    objc_setAssociatedObject(self, @selector(dispatchSource), dispatchSource, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (dispatch_source_t)dispatchSource {
    
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setGlowColor:(UIColor *)glowColor {
    
    objc_setAssociatedObject(self, @selector(glowColor), glowColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *)glowColor {
    
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setGlowOpacity:(NSNumber *)glowOpacity {
    
    objc_setAssociatedObject(self, @selector(glowOpacity), glowOpacity, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)glowOpacity {
    
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setGlowRadius:(NSNumber *)glowRadius {
    
    objc_setAssociatedObject(self, @selector(glowRadius), glowRadius, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)glowRadius {
    
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setGlowAnimationDuration:(NSNumber *)glowAnimationDuration {
    
    objc_setAssociatedObject(self, @selector(glowAnimationDuration), glowAnimationDuration, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)glowAnimationDuration {
    
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setGlowDuration:(NSNumber *)glowDuration {
    
    objc_setAssociatedObject(self, @selector(glowDuration), glowDuration, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)glowDuration {
    
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setHideDuration:(NSNumber *)hideDuration {
    
    objc_setAssociatedObject(self, @selector(hideDuration), hideDuration, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)hideDuration {
    
    return objc_getAssociatedObject(self, _cmd);
}

NSString * const _recognizerGlowLayer = @"_recognizerGlowLayer";

- (void)setGlowLayer:(CALayer *)glowLayer {
    
    objc_setAssociatedObject(self, @selector(glowLayer), glowLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CALayer *)glowLayer {
    
    return objc_getAssociatedObject(self, _cmd);
}

@end

源码已经放在GitHub:https://github.com/Fendouzhe/LRAnimations,有兴趣的欢迎下载查看,有帮助可以star,谢谢!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,463评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,868评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,213评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,666评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,759评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,725评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,716评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,484评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,928评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,233评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,393评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,073评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,718评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,308评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,538评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,338评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,260评论 2 352

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,028评论 25 707
  • 有个落魄的朋友 遭遇了离异 破产 再创业 一年过去了,终于好起来了。起码在我眼里,离暴富不远了。 开了一家教育机构...
    旧城xu阅读 383评论 0 0
  • 最后看完这部电影时,我的第一感觉是,敬佩为了国家而战的每一个地球人。每每看世界性的比赛,而中国选手获奖时,这...
    晋哥哥jasmine阅读 179评论 0 0
  • 我这一代人,如果出生在北方农村,尤其是华北和东北,小时候大约都睡过火炕吧!农业现代化发展迅速,但最先变化总是新的一...
    青蔌阅读 676评论 1 3
  • 使用css: #myimage {width:400px;height:300px;background:url(...
    mindy1031阅读 808评论 2 2