辉光UIView的category

本人视频教程系类   iOS中CALayer的使用

效果如下:

源码:

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 <UIKit/UIKit.h> @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 <objc/runtime.h> #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(, );
glowLayer.shadowColor = (color == nil ? [UIColor redColor].CGColor : color.CGColor);
glowLayer.shadowRadius = (radius > ? radius : .f);
glowLayer.opacity = .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, , , dispatch_get_main_queue());
dispatch_source_set_timer(self.dispatchSource, dispatch_time(DISPATCH_TIME_NOW, ),
NSEC_PER_SEC * (self.GCDTimerInterval == nil ? : self.GCDTimerInterval.floatValue), );
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 = @(.f);
animation.toValue = [NSNumber numberWithFloat:self.glowLayerOpacity.floatValue];
layer.opacity = self.glowLayerOpacity.floatValue;
} else {
animation.fromValue = @(.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 = @(.f); if (self.glowDuration != nil) {
animation.duration = self.glowDuration.floatValue;
layer.opacity = .f;
} else {
animation.duration = 0.8;
layer.opacity = .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 = @(.f);
animation.toValue = [NSNumber numberWithFloat:self.glowLayerOpacity.floatValue];
layer.opacity = self.glowLayerOpacity.floatValue;
} else {
animation.fromValue = @(.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 = @(.f); if (self.glowDuration != nil) {
animation.duration = self.glowDuration.floatValue;
layer.opacity = .f;
} else {
animation.duration = 0.8;
layer.opacity = .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(, , , )];
label.center = self.view.center;
label.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:.f];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"YouXianMing";
label.textColor = [UIColor redColor]; label.GCDTimerInterval = @(.f);
label.glowDuration = @(.f);
label.glowLayerOpacity = @(0.8f); [label createGlowLayerWithColor:[UIColor yellowColor] glowRadius:.f];
[label startGlow]; [self.view addSubview:label]; } @end

辉光UIView的category的更多相关文章

  1. 辉光的UIView

    辉光的UIView 辉光UIView使用了一个UIView的一个category,名为UIView+Glow,请自行到github上查找. 源码如下: // // RootViewController ...

  2. Shimmer辉光动画效果

    Shimmer辉光动画效果 效果 源码 https://github.com/facebook/Shimmer https://github.com/YouXianMing/Animations // ...

  3. 使用CALayer制作View的辉光效果

    使用CALayer制作View的辉光效果 实现以下的辉光效果: 思路是这样子的: 1. 创建好需要实现辉光效果的View 2. 对这个View进行截图 3. 将这个截图重新添加进View中 4. 对这 ...

  4. 支持辉光效果的Label

    支持辉光效果的Label 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 FBGlowLabel // // FBGlo ...

  5. GraphicsLab Project之辉光(Glare,Glow)效果 【转】

    作者:i_dovelemon 日期:2016 / 07 / 02 来源:CSDN 主题:Render to Texture, Post process, Glare, Glow, Multi-pass ...

  6. iOS之UI--辉光动画

    前言:学习来自YouXianMing老师的博客:<辉光UIView的category>以及YouXianMing老师的github源码:< GlowView >    而我个人 ...

  7. iOS动画1 — UIView动画

    iOS动画基础是Core Animation核心动画.Core Animation是iOS平台上负责图形渲染与动画的基础设施.由于核心动画的实现比较复杂,苹果提供了实现简单动画的接口—UIView动画 ...

  8. RCLighting

    RCLighting https://github.com/RidgeCorn/RCLighting 效果: 真机测试的效率: 看了源码,其实原理很简单: ====================== ...

  9. iOS-UI-UI控件概述

    以下列举一些在开发中可能用得上的UI控件: IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @p ...

随机推荐

  1. Chrome 谷歌如何快速实现跨域

    第一步:在你的E盘或者其他盘新建一个文件夹,命名为:E:\MyChromeDevUserData 第二步:找到你的谷歌浏览器快捷图标,鼠标右键选择属性,出现以下界面: 第三步:在目标选项的最后添加:  ...

  2. C#语法之Linq查询基础一

    Linq做.Net开发的应该都用过,有些地方很复杂的逻辑用Linq很方便的解决.对于Linq to object.Linq to xml.Linq to sql.Linq to Entity(EF)都 ...

  3. WCF异常信息

    1.服务“CJ.Demo.Conso.WcfService.EmployeeMngService”有零个应用程序(非基础结构)终结点.这可能是因为未找到应用程序的配置文件,或者在配置文件中未找到与服务 ...

  4. javascript加上标签

    在javascript脚本中可以加上标签,方便javascript程序进行快速定位,标签一般由一个合法的字符名称加上一个冒号组成,标签可以放在任意行的位置,这样可以为该行设置一个标记,然后再 结构体中 ...

  5. GDC NEC单机自动化设置

    GDC NEC 单机自动化设置 进入播放列表   进入设置,进入登陆,请选择维修员登陆,输入密码257910   选择“一般选项”中的“自动化” 在进入的新菜单中选择“设备”,添加一个新的名称,默认的 ...

  6. Xcode 8 媒体权限

  7. maven-shade-plugin 打包出错

    一般maven-shade-plugin 打包出错的原因都是因为jar包出错,一般使用mvn package -X 即可找出对应错误的jar包删除即可.我自己遇到的是打开自己打包完的jar包出错,整的 ...

  8. SQL Server 数据库差异 查询

    -- 比较两个数据库中表的差异 -- u表,p存储过程,v视图 -- INTFSIMSNEW新库,INTFSIMS旧库 SELECT NTABLE = A.NAME, OTABLE = B.NAME ...

  9. Scala安装时的坑

    重新安装了Scala(2.11.12版本)到d:\Program Files下,查看版本时,报如下异常: 百度了一下,竟然是不允许空格,太low了. 装到d:\Scala后问题解决

  10. Android SimpleAdapter的参数

    1.作用是ArrayList和 ListView的桥梁.这个ArrayList里边的每一项都是一个Map<String,?>类型.       ArrayList当中的每一项 Map对象都 ...