首页 > 其他 > 详细

使用CALayer制作View的辉光效果

时间:2014-06-10 21:59:26      阅读:397      评论:0      收藏:0      [点我收藏+]

使用CALayer制作View的辉光效果

bubuko.com,布布扣

实现以下的辉光效果:

bubuko.com,布布扣

思路是这样子的:

1. 创建好需要实现辉光效果的View

2. 对这个View进行截图

3. 将这个截图重新添加进View中

4. 对这个截图实现改变透明度的动画

 

ViewController.m

bubuko.com,布布扣
//
//  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
bubuko.com,布布扣

BackedImage.h

bubuko.com,布布扣
//
//  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
bubuko.com,布布扣

BackedImage.m

bubuko.com,布布扣
//
//  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
bubuko.com,布布扣

 

 

 

 

 

 

 

 

使用CALayer制作View的辉光效果,布布扣,bubuko.com

使用CALayer制作View的辉光效果

原文:http://www.cnblogs.com/YouXianMing/p/3779328.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!