博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
辉光UIView的category
阅读量:5318 次
发布时间:2019-06-14

本文共 9638 字,大约阅读时间需要 32 分钟。

辉光UIView的category

 

 本人视频教程系类   

 

效果如下:

源码:

UIView+GlowView.h 与 UIView+GlowView.m

////  UIView+GlowView.h//  YouXianClock////  Created by YouXianMing on 14-12-21.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import 
@interface UIView (GlowView)@property (nonatomic, strong) NSNumber *GCDTimerInterval; // 定时器的时间间隔,给float值@property (nonatomic, strong) NSNumber *glowDuration; // layer动画的时间长度,给float值@property (nonatomic, strong) NSNumber *glowLayerOpacity; // 设置glowLayer的动画透明度的程度,给float值,范围在[0,1]- (void)createGlowLayerWithColor:(UIColor *)color glowRadius:(CGFloat)radius;- (void)startGlow;- (void)glowToGlowLayerOnce;- (void)glowToNormalLayerOnce;@end
////  UIView+GlowView.m//  YouXianClock////  Created by YouXianMing on 14-12-21.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "UIView+GlowView.h"#import 
#define GLOWVIEW_LAYER_FLAG @"UIView+GlowView"@interface UIView ()@property (strong, nonatomic) dispatch_source_t dispatchSource;@property (strong, nonatomic) NSNumber *glowViewShowFlag;@end@implementation UIView (GlowView)#pragma mark - 动态添加了属性static char dispatchSourceTimerFlag;- (void)setDispatchSource:(dispatch_source_t)dispatchSource { objc_setAssociatedObject(self, &dispatchSourceTimerFlag, dispatchSource, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (dispatch_source_t)dispatchSource { return objc_getAssociatedObject(self, &dispatchSourceTimerFlag);}static char charGlowViewShowFlag;- (void)setGlowViewShowFlag:(NSNumber *)glowViewShowFlag { objc_setAssociatedObject(self, &charGlowViewShowFlag, glowViewShowFlag, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (NSNumber *)glowViewShowFlag { return objc_getAssociatedObject(self, &charGlowViewShowFlag);}static char GCDTimerIntervalFlag;- (void)setGCDTimerInterval:(NSNumber *)GCDTimerInterval { objc_setAssociatedObject(self, &GCDTimerIntervalFlag, GCDTimerInterval, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (NSNumber *)GCDTimerInterval { return objc_getAssociatedObject(self, &GCDTimerIntervalFlag);}static char glowLayerOpacityFlag;- (void)setGlowLayerOpacity:(NSNumber *)glowLayerOpacity { objc_setAssociatedObject(self, &glowLayerOpacityFlag, glowLayerOpacity, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (NSNumber *)glowLayerOpacity { return objc_getAssociatedObject(self, &glowLayerOpacityFlag);}static char glowDurationFlag;- (void)setGlowDuration:(NSNumber *)glowDuration { objc_setAssociatedObject(self, &glowDurationFlag, glowDuration, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (NSNumber *)glowDuration { return objc_getAssociatedObject(self, &glowDurationFlag);}#pragma mark - 方法- (void)createGlowLayerWithColor:(UIColor *)color glowRadius:(CGFloat)radius { UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIBezierPath* path = \ [UIBezierPath bezierPathWithRect:(CGRect){CGPointZero, CGSizeMake(self.bounds.size.width, self.bounds.size.height)}]; [color setFill]; [path fillWithBlendMode:kCGBlendModeSourceAtop alpha:1.0]; CALayer *glowLayer = [CALayer layer]; glowLayer.name = GLOWVIEW_LAYER_FLAG; glowLayer.frame = self.bounds; glowLayer.contents = (__bridge id)UIGraphicsGetImageFromCurrentImageContext().CGImage; glowLayer.shadowOpacity = 1.0f; glowLayer.shadowOffset = CGSizeMake(0, 0); glowLayer.shadowColor = (color == nil ? [UIColor redColor].CGColor : color.CGColor); glowLayer.shadowRadius = (radius > 0 ? radius : 2.f); glowLayer.opacity = 0.f; // 开始时候的透明度为0 [self.layer addSublayer:glowLayer]; UIGraphicsEndImageContext();}- (void)startGlow { [self.layer.sublayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { CALayer *layer = obj; // 找到了layer才进行下面的操作 if ([layer.name isEqualToString:GLOWVIEW_LAYER_FLAG]) { if (self.glowViewShowFlag == nil) { self.glowViewShowFlag = [NSNumber numberWithBool:NO]; } if (self.dispatchSource == nil) { self.dispatchSource = \ dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); dispatch_source_set_timer(self.dispatchSource, dispatch_time(DISPATCH_TIME_NOW, 0), NSEC_PER_SEC * (self.GCDTimerInterval == nil ? 1 : self.GCDTimerInterval.floatValue), 0); dispatch_source_set_event_handler(self.dispatchSource, ^{ if (self.glowViewShowFlag.boolValue == NO) { self.glowViewShowFlag = @(YES); // 做动画,从透明到显示出来 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; if (self.glowLayerOpacity != nil) { animation.fromValue = @(0.f); animation.toValue = [NSNumber numberWithFloat:self.glowLayerOpacity.floatValue]; layer.opacity = self.glowLayerOpacity.floatValue; } else { animation.fromValue = @(0.f); animation.toValue = @(0.8f); layer.opacity = 0.8; } if (self.glowDuration != nil) { animation.duration = self.glowDuration.floatValue; } else { animation.duration = 0.8; } [layer addAnimation:animation forKey:nil]; } else { self.glowViewShowFlag = @(NO); // 做动画 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; animation.fromValue = [NSNumber numberWithFloat:layer.opacity]; animation.toValue = @(0.f); if (self.glowDuration != nil) { animation.duration = self.glowDuration.floatValue; layer.opacity = 0.f; } else { animation.duration = 0.8; layer.opacity = 0.f; } [layer addAnimation:animation forKey:nil]; } }); dispatch_resume(self.dispatchSource); } } }];}- (void)glowToGlowLayerOnce { [self.layer.sublayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { CALayer *layer = obj; // 找到了layer才进行下面的操作 if ([layer.name isEqualToString:GLOWVIEW_LAYER_FLAG]) { // 做动画,从透明到显示出来 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; if (self.glowLayerOpacity != nil) { animation.fromValue = @(0.f); animation.toValue = [NSNumber numberWithFloat:self.glowLayerOpacity.floatValue]; layer.opacity = self.glowLayerOpacity.floatValue; } else { animation.fromValue = @(0.f); animation.toValue = @(0.8f); layer.opacity = 0.8; } if (self.glowDuration != nil) { animation.duration = self.glowDuration.floatValue; } else { animation.duration = 0.8; } [layer addAnimation:animation forKey:nil]; } }];}- (void)glowToNormalLayerOnce { [self.layer.sublayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { CALayer *layer = obj; // 做动画 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; animation.fromValue = [NSNumber numberWithFloat:layer.opacity]; animation.toValue = @(0.f); if (self.glowDuration != nil) { animation.duration = self.glowDuration.floatValue; layer.opacity = 0.f; } else { animation.duration = 0.8; layer.opacity = 0.f; } [layer addAnimation:animation forKey:nil]; }];}@end

使用时候的源码:

////  ViewController.m//  Glow////  Created by YouXianMing on 14/12/21.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "ViewController.h"#import "UIView+GlowView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];     self.view.backgroundColor = [UIColor blackColor];        // 普通label    UILabel *label      = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];    label.center        = self.view.center;    label.font          = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:50.f];    label.textAlignment = NSTextAlignmentCenter;    label.text          = @"YouXianMing";    label.textColor     = [UIColor redColor];            label.GCDTimerInterval = @(2.f);    label.glowDuration     = @(1.f);    label.glowLayerOpacity = @(0.8f);    [label createGlowLayerWithColor:[UIColor yellowColor] glowRadius:4.f];    [label startGlow];        [self.view addSubview:label];    }@end

 

转载于:https://www.cnblogs.com/YouXianMing/p/4177244.html

你可能感兴趣的文章
leetcode Letter Combinations of a Phone Number
查看>>
Unity 5.4 测试版本新特性---因吹丝停
查看>>
7.5 文件操作
查看>>
MyEclipse中将普通Java项目convert(转化)为Maven项目
查看>>
node js 安装.node-gyp/8.9.4 权限 无法访问
查看>>
windows基本命令
查看>>
VMware中CentOS设置静态IP
查看>>
[poj1006]Biorhythms
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>
Hover功能
查看>>
js千分位处理
查看>>
Mac---------三指拖移
查看>>
字符串类型的相互转换
查看>>
HTTP状态码
查看>>
iOS如何过滤掉文本中特殊字符
查看>>
基础学习:C#中float的取值范围和精度
查看>>
MongoDB-CRUD
查看>>
javaagent 简介
查看>>
python升级安装后的yum的修复
查看>>
Vim配置Node.js开发工具
查看>>