maskView配合带alpha通道图片
1.比CAGradientLayer高效
2.可以加载多张图片,并实现动画特效
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
backgroundView.image = [UIImage imageNamed:@"background"];
backgroundView.center = self.view.center;
[self.view addSubview:backgroundView];
UIImageView *baseView = [[UIImageView alloc] initWithFrame:backgroundView.frame];
baseView.image = [UIImage imageNamed:@"base"];
[self.view addSubview:baseView];
//创建maskView作为容器
UIView *mask = [[UIView alloc] initWithFrame:baseView.bounds];
baseView.maskView = mask;
//maskView中的subView1
UIImageView *picOne = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 400)];
picOne.image = [UIImage imageNamed:@"1"];
[mask addSubview:picOne];
//maskView中的subView2
UIImageView *picTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, 100, 400)];
picTwo.image = [UIImage imageNamed:@"2"];
[mask addSubview:picTwo];
[UIView animateWithDuration:2.f animations:^{
CGRect oneFrame = picOne.frame;
oneFrame.origin.y -= 400;
picOne.frame = oneFrame;
CGRect twoFrame = picTwo.frame;
twoFrame.origin.y -= 400;
picTwo.frame = twoFrame;
}];
github上开源的代码可查看