zoukankan      html  css  js  c++  java
  • 使用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"
    #import "BackedImage.h"
    #import "YXGCD.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) GCDTimer  *timer;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor blackColor];
    
        // 创建Label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        label.text = @"You:Xian:Ming";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:40.f];
        label.textColor = [UIColor redColor ];
        label.center = self.view.center;
        [self.view addSubview:label];
        
        // 将Label转换成Image
        BackedImage *backedImage = [BackedImage new];
        [backedImage createBackedLayerWithView:label
                                     withColor:[UIColor cyanColor]
                                  shadowRadius:5.f];
        CALayer *myLayer = backedImage.backedLayer;
        
        // 将这个layer添加进Label中
        [label.layer addSublayer:myLayer];
        
        // 开始辉光动画
        _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
        [_timer event:^{
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
            
            static int i = 0;
            if (i++ % 2 == 0)
            {
                animation.fromValue = [NSNumber numberWithFloat:1.f];
                animation.toValue   = [NSNumber numberWithFloat:0.f];
                animation.duration  = 0.8;
                myLayer.opacity = 0.f;
                [myLayer addAnimation:animation forKey:nil];
            }
            else
            {
                animation.fromValue = [NSNumber numberWithFloat:0.f];
                animation.toValue   = [NSNumber numberWithFloat:1.f];
                animation.duration  = 0.8;
                myLayer.opacity = 1.f;
                [myLayer addAnimation:animation forKey:nil];
            }
            
        } timeInterval: NSEC_PER_SEC * 1];
        [_timer start];
    }
    
    @end

    BackedImage.h

    //
    //  BackedImage.h
    //  Copyright (c) 2014年 Nick Jensen. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface BackedImage : NSObject
    
    @property (nonatomic, strong, readonly) CALayer  *backedLayer;
    
    - (void)createBackedLayerWithView:(UIView *)view
                            withColor:(UIColor *)color
                         shadowRadius:(CGFloat)radius;
    
    @end

    BackedImage.m

    //
    //  BackedImage.m
    //  Copyright (c) 2014年 Nick Jensen. All rights reserved.
    //
    
    #import "BackedImage.h"
    
    @implementation BackedImage
    
    - (void)createBackedLayerWithView:(UIView *)view
                            withColor:(UIColor *)color
                         shadowRadius:(CGFloat)radius
    {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO,
                                               [UIScreen mainScreen].scale);
        
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        
        UIBezierPath* path = [UIBezierPath bezierPathWithRect:(CGRect){CGPointZero,
            CGSizeMake(view.bounds.size.width, view.bounds.size.height)}];
        
        [color setFill];
        
        [path fillWithBlendMode:kCGBlendModeSourceAtop alpha:1.0];
        
        _backedLayer = [CALayer layer];
        _backedLayer.frame = view.bounds;
        _backedLayer.contents = (__bridge id)UIGraphicsGetImageFromCurrentImageContext().CGImage;
        _backedLayer.shadowOpacity = 1.0f;
        _backedLayer.shadowOffset  = CGSizeMake(0, 0);
        _backedLayer.shadowColor   = color.CGColor;
        _backedLayer.shadowRadius  = radius;
        
        UIGraphicsEndImageContext();
    }
    
    @end
  • 相关阅读:
    基于term vector深入探查数据
    oracle 行转列
    oracle 统计成绩
    最全最新个税计算公式---今天你税了吗?
    .net反混淆脱壳工具de4dot的使用
    使用ILSpy软件反编译.Net应用程序的方法及注意事项
    EmguCV使用Stitcher类来拼接图像
    从睡姿就可以看出你的性格,据说非常准,快存!
    分享几个.NET WinForm开源组件,纪念逐渐远去的WinForm。。。
    【转载】关于.NET下开源及商业图像处理(PSD)组件
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3779328.html
Copyright © 2011-2022 走看看