2014-10-10 61 views
1

我想要在蒙版中放置一个蒙版。出现任何经过蒙版后面传递的图像视图。所以这里是我的代码到目前为止,但这个代码的问题是它动画蒙版与图像视图。用蒙版动画UIImageView,但蒙版是静态的

UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.worldImageView.frame.size.height, self.worldImageView.frame.size.height)]; 

// ...position maskView... 
maskView.layer.position = CGPointMake(CGRectGetMidX([self.view bounds]), CGRectGetMidY([self.view bounds])); 

maskView.layer.backgroundColor = [UIColor blackColor].CGColor; 

maskView.layer.cornerRadius = 90; 

self.worldImage = [[UIImageView alloc] init]; 
self.worldImage.frame = self.worldImageView.frame; 
self.worldImage.image = [UIImage imageNamed:@"continents.png"]; 
[maskView addSubview:self.worldImage]; 

[self.view addSubview:maskView]; 

CABasicAnimation* worldAnimation = [[CABasicAnimation alloc] init]; 
[worldAnimation setFromValue:[NSValue valueWithCGPoint:CGPointMake(self.view.frame.size.width + 50, CGRectGetMidY([maskView bounds]))]]; 
[worldAnimation setToValue:[NSValue valueWithCGPoint:CGPointMake(-50, CGRectGetMidY([maskView bounds]))]]; 
worldAnimation.keyPath = @"position"; 
worldAnimation.duration = 6.0f; 
worldAnimation.repeatCount = HUGE_VALF; 
[[self.worldImage layer] addAnimation:worldAnimation forKey:nil]; 

我不知道我是否解释得很好。请告诉我,如果你需要更多的信息。先谢谢你!

+0

你说的是面具随图像移动,但你不想要它? – ravron 2014-10-11 01:38:36

+0

是的图像视图在动画时仍然被遮罩。 – PoKoBros 2014-10-11 01:44:08

+0

好的,但你想要做什么?你是说你想让面具代表的“窗口”静止不动,而图像在它后面移动? – ravron 2014-10-11 01:46:05

回答

2

您的问题在于的mask属性附加到那个UIView - 它们不能单独移动。从技术上讲,它实际上是连接到视图的底层,但是无论如何。

如果您希望图像看起来像是在圆形窗口前传递的(以便您可以随时看到它),您需要将其作为视图的子视图添加蒙版。所以,你可能做这样的事情:

UIView *maskView = [[UIView alloc] init]; 

// ...position maskView... 

maskView.layer.cornerRadius = 90; 
maskView.clipsToBounds = YES; 

self.worldImage = [[UIImageView alloc] init]; 
self.worldImage.frame = self.worldImageView.frame; 
self.worldImage.image = [UIImage imageNamed:@"continents.png"]; 
[maskView addSubview:self.worldImage]; 

[self.view addSubview:maskView]; 

// ...animate worldImage back and forth... 

注意,而是采用了CAShapeLayer,我干脆把圆角上maskView;既然你想要一个圆形的面具,这是一个更简单的把戏。另外,当动画worldImage时,请考虑使用UIView的基于块的动画方法之一 - 它们对于这样简单的东西更容易处理。

+0

我在我的问题中更新了我的代码。该视图不掩盖图像视图。我究竟做错了什么? – PoKoBros 2014-10-11 02:48:42

+0

哎呀!我忘了,必须设置'maskView.clipsToBounds = YES'。 – ravron 2014-10-11 05:35:44

+0

非常感谢!它完美的作品。 – PoKoBros 2014-10-11 14:10:44