2011-04-18 108 views
8

你會如何去覆蓋UIScrollView上的各種遮罩圖像?UIScrollView上的遮罩覆蓋圖

例如,我有一個圖像,左邊和右邊的黑色漸漸變成中間的白色。我想使用它,以便我的滾動視圖中的項目逐漸淡出到兩側,而中間項目完全不透明。

此外,scrollview所放置的視圖的背景是動態的並且可以更改的圖像(不是純色)。

任何想法?

回答

0

如果您在滾動視圖後面有純色背景,只需在滾動視圖頂部疊加圖像,而不是嘗試在其中執行蒙版,就會獲得更好的性能。

+0

是的,我在想那個。不幸的是,在這種情況下,我需要一張圖片作爲背景,而背景圖片也必須是動態的。 – Nick 2011-04-18 22:41:27

0

嘗試一些沿着這些路線:

// I assume you are in a UIViewController and self.view is set to some kind of view 

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; 
scrollView.backgroundColor = [UIColor redColor]; 
[self.view addSubview: scrollView]; 
[scrollView release]; 

// add img with gradient 
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"my-gradient.png"]; 
imgView.frame = CGRectMake(0, 0, 160, 420); 
[self.view addSubview: imgView]; 
[imgView release]; 

這將給你開始左邊,去一路中心,爲整個高度,假設一個漸變,「我-gradient.png」是實際包含漸變的圖像。

0

這些操作是消費者的大消耗。最好的辦法是(如果你的情況允許)在你的UIScrollView上放置一個蒙版圖像。這個圖像的背景應該是透明的(例如png32),圖像應該是一個alpha梯度。

0

使用insertView:yourView atIndex:0

4

你的問題結束,目前我能找到的基本問題的最好來源,所以我決定在這裏發佈我的解決方案。

如果您將遮罩直接添加到滾動視圖,則由於某種原因將遮罩應用於內容本身 - 我。即當您滾動時,透明度不會移動。根據其他帖子的建議,我將漸變放在視圖上(或者說,包含)滾動視圖。因此,scrollContainer是一個UIViewObject,它的唯一子視圖就是所討論的滾動視圖。

此遮罩配置爲從左到右滾動。您可以通過操縱漸變的開始和結束點屬性將其更改爲從上到下。

其中一個缺點是,它也會剪輯滾動視圖的滾動條位置指示器。由於我的場景不需要,這是可以接受的,但是你的里程可能會有所不同。

CAGradientLayer *gradient = [CAGradientLayer layer]; 
gradient.frame = scrollView.bounds; 
gradient.colors = [NSArray arrayWithObjects: 
        (id)[[UIColor colorWithWhite:0 alpha:0] CGColor], 
        (id)[[UIColor colorWithWhite:0 alpha:1] CGColor], 
        (id)[[UIColor colorWithWhite:0 alpha:1] CGColor], 
        (id)[[UIColor colorWithWhite:0 alpha:0] CGColor], nil]; 
gradient.locations=[NSArray arrayWithObjects: 
        [NSNumber numberWithFloat:0], 
        [NSNumber numberWithFloat:.1], 
        [NSNumber numberWithFloat:.9], 
        [NSNumber numberWithFloat:1], nil]; 
gradient.startPoint=CGPointMake(0, .5); 
gradient.endPoint=CGPointMake(1, .5); 
[scrollContainer.layer setMask:gradient]; 
+0

確保你的鏈接框架和庫中有QuartzCore – 2013-04-27 22:23:31