GlowView

效果

说明

这是本人第二次写辉光view了,这是改进版本

源码

https://github.com/YouXianMing/UI-Component-Collection

//
// UIView+GlowView.h
// GlowView
//
// Created by YouXianMing on 15/7/4.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (GlowView) //
// == 动画时间解析 ==
//
// 0.0 ----------- 0.0 ------------> glowOpacity [---------------] glowOpacity ------------> 0.0
// T T T T
// | | | |
// | | | |
// . . . .
// hideDuration animationDuration glowDuration animationDuration
// #pragma mark - 设置辉光效果
/**
* 辉光的颜色
*/
@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)glowToshow; /**
* 隐藏辉光
*/
- (void)glowToHide; /**
* 开始循环辉光
*/
- (void)startGlowLoop; @end
//
// UIView+GlowView.m
// GlowView
//
// Created by YouXianMing on 15/7/4.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #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 = .f;
self.glowLayer.shadowOffset = CGSizeMake(, );
self.glowLayer.shadowOpacity = .f; UIGraphicsEndImageContext();
} - (void)insertGlowLayer { if (self.glowLayer) {
[self.layer addSublayer:self.glowLayer];
}
} - (void)removeGlowLayer { if (self.glowLayer) {
[self.glowLayer removeFromSuperlayer];
}
} - (void)glowToshow { self.glowLayer.shadowColor = [self accessGlowColor].CGColor;
self.glowLayer.shadowRadius = [self accessGlowRadius].floatValue; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; animation.fromValue = @(.f);
animation.toValue = [self accessGlowOpacity];
self.glowLayer.opacity = [self accessGlowOpacity].floatValue;
animation.duration = [self accessAnimationDuration].floatValue; [self.glowLayer addAnimation:animation forKey:nil];
} - (void)glowToHide { self.glowLayer.shadowColor = [self accessGlowColor].CGColor;
self.glowLayer.shadowRadius = [self accessGlowRadius].floatValue; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; animation.fromValue = [self accessGlowOpacity];
animation.toValue = @(.f);
self.glowLayer.opacity = .f;
animation.duration = [self accessAnimationDuration].floatValue; [self.glowLayer addAnimation:animation forKey:nil];
} - (void)startGlowLoop { if (self.dispatchSource == nil) { CGFloat seconds = [self accessAnimationDuration].floatValue * + [self accessGlowDuration].floatValue + [self accessHideDuration].floatValue;
CGFloat delaySeconds = [self accessAnimationDuration].floatValue + [self accessGlowDuration].floatValue; 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 * seconds, );
dispatch_source_set_event_handler(self.dispatchSource, ^{ [self glowToshow]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * delaySeconds), dispatch_get_main_queue(), ^{ [self glowToHide];
});
}); dispatch_resume(self.dispatchSource); }
} #pragma mark - 处理数据越界问题 - (NSNumber *)accessGlowOpacity { if (self.glowOpacity) { if (self.glowOpacity.floatValue <= || self.glowOpacity.floatValue > ) {
return @(0.8);
} else {
return self.glowOpacity;
} } else { return @(0.8);
}
} - (NSNumber *)accessGlowDuration { if (self.glowDuration) { if (self.glowDuration.floatValue <= ) {
return @(0.5f);
} else {
return self.glowDuration;
} } else { return @(0.5f);
}
} - (NSNumber *)accessHideDuration { if (self.hideDuration) { if (self.hideDuration.floatValue < ) {
return @(0.5);
} else {
return self.hideDuration;
}
} else { return @(0.5f);
}
} - (NSNumber *)accessAnimationDuration { if (self.glowAnimationDuration) { if (self.glowAnimationDuration.floatValue <= ) {
return @(.f);
} else {
return self.glowAnimationDuration;
} } else { return @(.f);
}
} - (UIColor *)accessGlowColor { if (self.glowColor) {
return self.glowColor;
} else {
return [UIColor redColor];
}
} - (NSNumber *)accessGlowRadius { if (self.glowRadius) { if (self.glowRadius.floatValue <= ) {
return @(.f);
} else {
return self.glowRadius;
}
} else {
return @(.f);
}
} /////////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - runtime属性 NSString * const _recognizerDispatchSource = @"_recognizerDispatchSource";
- (void)setDispatchSource:(dispatch_source_t)dispatchSource { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerDispatchSource), dispatchSource, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (dispatch_source_t)dispatchSource { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerDispatchSource));
} NSString * const _recognizerGlowColor = @"_recognizerGlowColor";
- (void)setGlowColor:(UIColor *)glowColor { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerGlowColor), glowColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (UIColor *)glowColor { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerGlowColor));
} NSString * const _recognizerGlowOpacity = @"_recognizerGlowOpacity";
- (void)setGlowOpacity:(NSNumber *)glowOpacity { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerGlowOpacity), glowOpacity, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSNumber *)glowOpacity { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerGlowOpacity));
} NSString * const _recognizerGlowRadius = @"_recognizerGlowRadius";
- (void)setGlowRadius:(NSNumber *)glowRadius { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerGlowRadius), glowRadius, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSNumber *)glowRadius { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerGlowRadius));
} NSString * const _recognizerGlowAnimationDuration = @"_recognizerGlowAnimationDuration";
- (void)setGlowAnimationDuration:(NSNumber *)glowAnimationDuration { objc_setAssociatedObject(self, (__bridge const void *)(glowAnimationDuration), _recognizerGlowAnimationDuration, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSNumber *)glowAnimationDuration { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerGlowAnimationDuration));
} NSString * const _recognizerGlowDuration = @"_recognizerGlowDuration";
- (void)setGlowDuration:(NSNumber *)glowDuration { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerGlowDuration), glowDuration, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSNumber *)glowDuration { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerGlowDuration));
} NSString * const _recognizerHideDuration = @"_recognizerHideDuration";
- (void)setHideDuration:(NSNumber *)hideDuration { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerHideDuration), hideDuration, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSNumber *)hideDuration { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerHideDuration));
} NSString * const _recognizerGlowLayer = @"_recognizerGlowLayer";
- (void)setGlowLayer:(CALayer *)glowLayer { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerGlowLayer), glowLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (CALayer *)glowLayer { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerGlowLayer));
} @end

[控件] GlowView的更多相关文章

  1. JS调用Android、Ios原生控件

    在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...

  2. HTML5 progress和meter控件

    在HTML5中,新增了progress和meter控件.progress控件为进度条控件,可表示任务的进度,如Windows系统中软件的安装.文件的复制等场景的进度.meter控件为计量条控件,表示某 ...

  3. 百度 flash html5自切换 多文件异步上传控件webuploader基本用法

    双核浏览器下在chrome内核中使用uploadify总有302问题,也不知道如何修复,之所以喜欢360浏览器是因为帮客户控制渲染内核: 若页面需默认用极速核,增加标签:<meta name=& ...

  4. JS与APP原生控件交互

    "热更新"."热部署"相信对于混合式开发的童鞋一定不陌生,那么APP怎么避免每次升级都要在APP应用商店发布呢?这里就用到了混合式开发的概念,对于电商网站尤其显 ...

  5. UWP开发必备:常用数据列表控件汇总比较

    今天是想通过实例将UWP开发常用的数据列表做汇总比较,作为以后项目开发参考.UWP开发必备知识点总结请参照[UWP开发必备以及常用知识点总结]. 本次主要讨论以下控件: GridView:用于显示数据 ...

  6. 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~

    一.写在前面 自使用android studio开始,就被它独特的依赖方式:compile 'com.android.support:appcompat-v7:25.0.1'所深深吸引,自从有了它,麻 ...

  7. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

  8. Windows API 设置窗口下控件Enable属性

    参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-put.html http://www.yuanjiaocheng.net/we ...

  9. VB.NET设置控件和窗体的显示级别

    前言:在用VB.NET开发射频检测系统ADS时,当激活已存在的目标MDI子窗体时,被其他子窗体遮住了,导致目标MDI子窗体不能显示. 这个问题怎么解决呢?网上看到一篇帖子VB.NET设置控件和窗体的显 ...

随机推荐

  1. nginx的gzip模块

    gzip模块是我们在nginx里面经常用到的,压缩响应的数据,这通常有助于将传输数据的大小减少一半甚至更多.可以让我们访问网站更为流畅. Syntax Default Context gzip on ...

  2. 使用Pelican在Github(国外线路访问)和Coding(国内线路访问)同步托管博客

    本文原文地址:使用Pelican在Github(国外线路访问)和Coding(国内线路访问)同步托管博客 介绍: Github Pages 禁用了百度爬虫,因此百度搜索引擎经常抓取不到在Github上 ...

  3. OOAD之单例模式Singleton的6种写法

    1  主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 一 :第一种 饿汉式(预加载) public class Singleton { private Singleton(){ ...

  4. elasticSearch6源码分析(2)模块化管理

    elasticsearch里面的组件基本都是用Guice的Injector进行注入与获取实例方式进行模块化管理. 在node的构造方法中 /** * Constructs a node * * @pa ...

  5. 编写高质量 JavaScript -- 知识点小记

    一: 团队合作避免JS冲突 脚本中的变量随时存在冲突的风险, 1.   解决办法---用匿名函数将脚本包起来,让变量的作用域控制在匿名函数之内 如: <script type="tex ...

  6. Kafka安装kafka-manager

    1 .下载kafka-manager 想要查看和管理Kafka,完全使用命令并不方便,我们可以使用雅虎开源的Kafka-manager,GitHub地址如下: https://github.com/y ...

  7. CSS3文本属性

    text-shadow(文本阴影) <h1>我有文字阴影</h1> h1{ color:#ffff00; text-shadow:1px 2px 2px #000000; } ...

  8. 入门redis

    学习了大佬的博客,来源自:https://www.cnblogs.com/5ishare/p/6280023.html 一.下载 https://github.com/ServiceStack/red ...

  9. MyEclipse中更改JRE环境

    今天代码中需要用到λ表达式,但λ表达式需要JRE1.8的支持,而MyEclipse设置的默认JRE是1.7.为了是程序能够顺利通过编译,需要将MyEclipse的JRE由1.7转换为1.8.步骤如下:

  10. Dubbo下载-从missing artifactId说起

    项目pom文件引入dubbo 报 missing artifactId https://github.com/dangdangdotcom/dubbox 从GitHub上直接下载解压包, 最好下载分支 ...