辉光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. 机器学习--boosting家族之XGBoost算法

    一.概念 XGBoost全名叫(eXtreme Gradient Boosting)极端梯度提升,经常被用在一些比赛中,其效果显著.它是大规模并行boosted tree的工具,它是目前最快最好的开源 ...

  2. Eth 部署智能合约

    首先要开发以太坊的智能合约,需要EVM(以太坊虚拟机),也就是需要运行的环境,我们可以通过 geth 来设置开发环境: geth --datadir testNet --dev console 2&g ...

  3. Go 协程编程感悟

    0.GPM模型 1.默认地, Go所有的goroutines只能在一个线程里跑 ,除非告诉Go我们允许同时最多使用多个核,或者主动让出CPU时间,使goroutine可以抢占式的执行 2.如果当前go ...

  4. Hive use mapreduce引擎 bsonFile splits报错处理

    Hive use mapreduce引擎 1.          出现报错.关于bsonFile的.禁止hdfs里面存储的bson文件自动splits. 可以在hive-site.xml文件里面添加如 ...

  5. gocommand:一个跨平台的golang命令行执行package

    最近在做一个项目的时候,需要使用golang来调用操作系统中的命令行,来执行shell命令或者直接调用第三方程序,这其中自然就用到了golang自带的exec.Command. 但是如果直接使用原生e ...

  6. 阿里巴巴Java开发手册(详尽版)-个人未注意到的知识点(转)

    转自 https://blog.csdn.net/u013039395/article/details/86528164 一.编程规约 (一) 命名风格 [强制]代码中的命名只可用英文方式 [强制]类 ...

  7. C# BBcode 转 Markdown

    本文告诉大家一个简单的方法从 BBcode 转为 Markdown 本文的方法都是使用正则转换,现在支持的代码只有很少的常用标签,如果大家发现有转换失败的,请帮我修改代码,估计代码我不会进行修改. 最 ...

  8. sqlserver数据导入导出问题

    sqlserver,如果用结果另存为,导出txt数据,然后在导入数据库,有时候会出问题,很难解决. 但是全选,右击,复制到自己创建的txt里面,在导入数据,就不会有问题的. 神奇,不知道为什么,但是能 ...

  9. SQL Server Profiler小技巧——筛选请求

    如果需要转载,请附上本文作者和原文链接:http://www.cnblogs.com/zeusro/p/4016228.html Microsoft SQL Server Profiler 是 SQL ...

  10. 第二节:Java开发环境的搭建

    一.认识并安装JDK 1.JDK(Java Development Kit)是Java开发工具集,包括Java运行环境(JRE).Java开发工具以及一些基础类库,进行Java开发所必须安装的软件. ...