码迷,mamicode.com
首页 > 其他好文 > 详细

CALayer实现遮罩效果

时间:2015-03-31 10:46:38      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)CALayer *imageLayer;
@property(nonatomic,strong)CALayer *maskLayer;
@property(nonatomic,strong)UIImage *imageContents;
@property(nonatomic,strong)UIImage *maskContents;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //获取图片
    self.imageContents=[UIImage imageNamed:@"1"];
    self.maskContents=[UIImage imageNamed:@"2"];
    
    
    //创建图片layer
    self.imageLayer=[CALayer layer];
    self.imageLayer.frame=CGRectMake(50, 50, 200, 200);
    self.imageLayer.contents=(__bridge id)self.imageContents.CGImage;
    [self.view.layer addSublayer:self.imageLayer];
    
    
    //创建出遮罩layer
    self.maskLayer=[CALayer layer];
    self.maskLayer.frame=self.imageLayer.bounds;
    self.maskLayer.contents=(__bridge id)self.maskContents.CGImage;
    
    //给图片layer提供遮罩的layer
    self.imageLayer.mask=self.maskLayer;
    
    //3秒后做做改变maskLayer的Frame动画
    [self performSelector:@selector(layerAnimation) withObject:nil afterDelay:3];
    
}
-(void)layerAnimation
{
    self.maskLayer.frame=CGRectMake(50, 50, 200, 200);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

@end

 

CALayer实现遮罩效果

标签:

原文地址:http://www.cnblogs.com/thbbsky/p/4380109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!