辉光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. Redis笔记(二):Redis数据类型

    Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...

  2. 基于Java的简易表达式解析工具(一)

    最近需要用到相关表达式解析的工具,然后去网上搜索,找到了一个用C#写的表达式解析工具,仔细看了功能后发现,这正是我需要的,如果我能将它改造成基于Java语言的方式,岂不是更好吗,所以花了一段时间,把网 ...

  3. 一个基于Socket的http请求监听程序实现

    首先来看以下我们的需求: 用java编写一个监听程序,监听指定的端口,通过浏览器如http://localhost:7777来访问时,可以把请求到的内容记录下来,记录可以存文件,sqlit,mysql ...

  4. DataTable的Ajax使用

    DataTable Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 官网: https://datatables.net/ 中文网: ...

  5. [BJOI 2018]求和

    Description 题库链接 给你一棵 \(n\) 个结点的有根树, \(m\) 次询问这棵树上一段路径上所有节点深度的 \(k\) 次方和. \(1\leq n\leq 300000,1\leq ...

  6. SQL Server附加数据库拒绝访问解决方法汇总

    @本文来自百度 方法一:修改权限法 1 打开要附加的数据库文件所在的文件夹,即扩展名为mdf的文件所在的文件夹,如下图所示: 2 右键单击mdf文件,选择“属性”,如下图所示: 3 单击“安全”选项卡 ...

  7. C 语言 static、extern与指针函数介绍

    1.exit(0)正常退出程序 exit(1)程序异常时退出程序 2.static(静态变量)修饰局部变量 在局部变量使用static修饰,会延长局部变量的存在期.但我们需要注意一下几点: 虽然sta ...

  8. 【转】sql server日期比较

    1. 当前系统日期.时间select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值例如:向日期加上2天select dateadd(day ...

  9. 哪个类可用于处理 Unicode?

    A. InputStreanReader的构造函数: InputStreamReader(InputStream in)          创建一个使用默认字符集的 InputStreamReader ...

  10. HDU 2588 GCD------欧拉函数变形

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...