2014-10-10 66 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