辉光UIView的category

辉光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(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
时间: 2024-11-18 06:43:04

辉光UIView的category的相关文章

辉光的UIView

辉光的UIView   辉光UIView使用了一个UIView的一个category,名为UIView+Glow,请自行到github上查找. 源码如下: // // RootViewController.m // GlowView // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import "UIView+Glow.h" #import

带辉光效果的跑马灯

带辉光效果的跑马灯   效果   说明 并没有对代码进行封装,以后会在项目 Animation(https://github.com/YouXianMing/Animations)里面进行集成,欢迎前去star.   源码 UIView+GlowView // // UIView+GlowView.h // GlowView // // Created by YouXianMing on 15/7/4. // Copyright (c) 2015年 YouXianMing. All rights

使用CALayer制作View的辉光效果

使用CALayer制作View的辉光效果 实现以下的辉光效果: 思路是这样子的: 1. 创建好需要实现辉光效果的View 2. 对这个View进行截图 3. 将这个截图重新添加进View中 4. 对这个截图实现改变透明度的动画   ViewController.m // // ViewController.m // // Copyright (c) 2013 Nick Jensen. All rights reserved. // #import "ViewController.h"

blender图片怎么给图片添加辉光效果?

  blender图片怎么给图片添加辉光效果?           1.打开blender切换到"节点编辑器". 2.底部切换到"合成处理",勾选"使用节点"."背景图"."自动渲染". 3.通过shift+a创建"图像"."辉光"."预览器". 4."图像"中打开自己的需要处理的图片. 5.首先把"图像".

代码-wpf按钮如何实现文字的辉光效果?

问题描述 wpf按钮如何实现文字的辉光效果? 我是希望当把鼠标移到按钮时,按钮的文字就能显示出辉光效果,希望大神能给出实现的代码和必要的注释,谢谢 解决方案 WPF实现下雪效果 ...WPF-WPF BitmapEffect (按钮凹凸效果)WPF拖拽效果实现

iOS之UI--辉光动画

前言:学习来自YouXianMing老师的博客:<辉光UIView的category >以及YouXianMing老师的github源码:< GlowView>    而我个人考虑到分类的二次拓展性(或者是再一次拓展)不是特别好,所以将YouXianMing老师的用分类拓展的辉光动画,改写成一个继承CALayer的可拓展的普通类. 一方面,也是作为自我训练编码,对辉光UIView的实现所使用到的上下文绘制.核心动画.GCD中的定时器以及Runtime动态添加属性等知识进一步的熟练运

Flash光特效综合分析

特效 在众多绚烂夺目的Flash效果中,最梦幻而神奇的效果应该算是光特效了.光特效是Flash中的终级境界--魔域,常见的有辉光.激光.投射线.集光束等等,这些光特效所带来的是极大的视觉冲击和动感享受.不过,很可惜的是,许多人始终无法掌握光特效的设计要素,或是以为只有通过ActionScript或者依赖别的软件才能完成这些特效.今天,笔者就带大家探索一下光特效的神奇奥秘!效果演示如下:   源文件下载 在Flash中,光亦可分为"点光源","线光源"和"面

RCLighting

RCLighting https://github.com/RidgeCorn/RCLighting 效果: 真机测试的效率: 看了源码,其实原理很简单: ================================ 1. 实现UIView的category 2. 继承了一个CALayer 3. 将这个继承的CALayer插入到UIView的subLayer中 4. 对这个layer进行动画操作 ================================ 源码: // // RootV

使用Facebook开源代码FBShimmering封装进度显示的ShimmeCircleView

使用Facebook开源代码FBShimmering封装进度显示的ShimmeCircleView 效果图: 静态图: 源码: ShimmeCircleView.h 与 ShimmeCircleView.m // // ShimmeCircleView.h // YouXianMingClock // // Created by YouXianMing on 14-10-13. // Copyright (c) 2014年 YouXianMing. All rights reserved. //