CALayer实现遮罩效果

#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

 

posted @ 2015-03-31 10:14  曹县三胖暴打大猩猩  阅读(236)  评论(0编辑  收藏  举报