iOS:旋转图像,但不旋转蒙版.怎么做这个



我制作了两个图像:一个是我的微调图像,另一个是蒙版图像(部分透明渐变)。想象一下:微调器图像顺时针旋转,底部有深色渐变(看起来像雾霾),旋转的微调器部分隐藏了这个渐变。这就是我的目标。

我为我的场景添加了两个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 的底部。我该怎么做?或者我的概念是错误的,如果是这样,给我一些指示,我该如何存档我想要的东西。随意询问。谢谢。

对我来说很棘手,但我做到了。

这是你需要做的

:你需要一个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;

就这样。希望这会帮助某人。

最新更新