我制作了两个图像:一个是旋转图像,另一个是掩膜图像(部分透明梯度)。想象一下:旋转图像是顺时针旋转的,底部有暗梯度(看上去像雾霾),旋转旋转的旋转器部分隐藏在这个梯度中。这就是我的目标。
我添加了两个UIImageView到我的场景,但我的面具图像也影响背景颜色,我不想那样。我想要我的面具图像只掩蔽旋转,所以我做了如下:
- (void)viewDidLoad
{
[super viewDidLoad];
//add mask to UIImageView
CALayer *mask = [CALayer layer];
mask.contents = (id)[[UIImage imageNamed:@"mask.png"] CGImage];
mask.frame = CGRectMake(0, 0, self.spinner.bounds.size.width,self.spinner.bounds.size.height);
self.spinner.layer.mask = mask;
self.spinner.layer.masksToBounds = YES;
//rotate UIImageView (trouble with mask, it must not be rotated)
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 20.0f;
animation.repeatCount = INFINITY;
[self.spinner.layer addAnimation:animation forKey:@"Spinner"];
}因此,我得到了良好的面罩图像,但我的面具旋转与我的旋转,我想总是把我的面具夹在我的旋转UIImageView底部。我该怎么做?或者我的想法是错误的,如果是的话,给我一些方向,我如何才能存档我想要的。你随便问吧。谢谢。
发布于 2014-03-02 21:02:19
对我来说很棘手,但我做到了。
您需要这样做:您需要一个UIView (作为UIImageView的容器)。
请在下面发布的源代码中更改此部分:
CALayer *mask = [CALayer layer];
mask.contents = (id)[[UIImage imageNamed:@"mask.png"] CGImage];
mask.frame = self.yourContainerView.bounds;
self.yourContainerView.layer.mask = mask;
self.yourContainerView.layer.masksToBounds = YES;就这样。希望这能帮上忙。
https://stackoverflow.com/questions/22131466
复制相似问题