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

示例代码

- (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 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。