码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 利用图层的mask制作无缝的过渡效果

时间:2015-05-04 15:20:23      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

示例代码

- (void)viewDidLoad {

    [super viewDidLoad];

 

    [self creatLayer];

    [self creatText];

    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(change) userInfo:nil repeats:YES];

}

 

- (void)creatLayer

{

    CALayer *left = [CALayer layer];

    left.frame = CGRectMake(100, 100, 200, 50);

    left.backgroundColor = [UIColor blueColor].CGColor;

    [self.view.layer addSublayer:left];

    self.left = left;

    

    CALayer *right = [CALayer layer];

    right.frame = CGRectMake(100, 100, 200, 50);

    right.backgroundColor = [UIColor redColor].CGColor;

    [self.view.layer addSublayer:right];

    self.right = right;

}

 

- (void)creatText

{

    UILabel * leftLabel =[[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];

    leftLabel.text = @"测试一下测试一下测试";

    leftLabel.textColor = [UIColor redColor];

    leftLabel.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:leftLabel];

    self.leftLabel = leftLabel;

    

    UILabel * rightLabel =[[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];

    rightLabel.text = @"测试一下测试一下测试";

    rightLabel.textColor = [UIColor blueColor];

    rightLabel.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:rightLabel];

    self.rightLabel = rightLabel;    

}

 

- (void)change

{

    self.bili = arc4random()%100;

 

    self.left.mask = [CALayer layer];

    self.left.mask.backgroundColor = [[UIColor blackColor] CGColor];

    self.left.mask.frame = CGRectMake(0, 0, 100+self.bili, 50);

    

    self.right.mask = [CALayer layer];

    self.right.mask.backgroundColor = [[UIColor blackColor ] CGColor];

    self.right.mask.frame = CGRectMake(100+self.bili, 0, 100- self.bili, 50);

    

    self.leftLabel.layer.mask = [CALayer layer];

    self.leftLabel.layer.mask.backgroundColor = [[UIColor blackColor] CGColor];

    self.leftLabel.layer.mask.frame = CGRectMake(0, 0, 100+self.bili, 50);

    

    self.rightLabel.layer.mask = [CALayer layer];

    self.rightLabel.layer.mask.backgroundColor = [[UIColor blackColor ] CGColor];

    self.rightLabel.layer.mask.frame = CGRectMake(100+self.bili, 0, 100- self.bili, 50);

}

技术分享  技术分享

 

KVNMaskedPageControl的实现原理也是如此:

http://www.oschina.net/p/kvnmaskedpagecontrol/similar_projects?lang=0&sort=view&p=1 

iOS 利用图层的mask制作无缝的过渡效果

标签:

原文地址:http://www.cnblogs.com/oumygade/p/4476133.html

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