的左/右側褪色梯度I具有水平滾動視圖控制器的,像這樣的主視圖(灰色是UIView的,木材是UICollectionView)內UICollectionView:目標C - 上UICollectionView
我想在這個UICollectionView的最左邊的最左邊添加固定的漸變漸變,以便在用戶滾動時滾動看起來消失。我會如何去做這件事?它是否涉及到CAGradientLayer的某些用途?我會很感激你能給我的任何幫助!
的左/右側褪色梯度I具有水平滾動視圖控制器的,像這樣的主視圖(灰色是UIView的,木材是UICollectionView)內UICollectionView:目標C - 上UICollectionView
我想在這個UICollectionView的最左邊的最左邊添加固定的漸變漸變,以便在用戶滾動時滾動看起來消失。我會如何去做這件事?它是否涉及到CAGradientLayer的某些用途?我會很感激你能給我的任何幫助!
嘗試添加CAGradientLayer兩層的集合視圖子層:
#import <QuartzCore/QuartzCore.h>
CAGradientLayer *leftShadow = [CAGradientLayer layer];
leftShadow.frame = CGRectMake(0, 0, 100, self.collectionView.frame.size.height);
leftShadow.startPoint = CGPointMake(0, 0.5);
leftShadow.endPoint = CGPointMake(1.0, 0.5);
leftShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];
[self.collectionView.layer addSublayer:leftShadow];
CAGradientLayer *rightShadow = [CAGradientLayer layer];
rightShadow.frame = CGRectMake(CGRectGetWidth(self.collectionView.frame)-100.0, 0, 100, self.collectionView.frame.size.height);
rightShadow.startPoint = CGPointMake(1.0, 0.5);
rightShadow.endPoint = CGPointMake(0, 0.5);
rightShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];
[self.collectionView.layer addSublayer:rightShadow];
我實際上設法想出解決辦法使用一個遮罩層感謝this tutorial at cocoanetics。下面是我所做的:
@interface ScalesViewController : UIViewController
{
CAGradientLayer *maskLayer;
}
@end
然後在.M,我放在以下:
- (void)viewDidAppear:(BOOL)animated
{
[super viewWillAppear: animated];
if (!maskLayer)
{
maskLayer = [CAGradientLayer layer];
CGColorRef outerColor = [[UIColor colorWithWhite:0.0 alpha:1.0] CGColor];
CGColorRef innerColor = [[UIColor colorWithWhite:0.0 alpha:0.0] CGColor];
maskLayer.colors = [NSArray arrayWithObjects:
(__bridge id)outerColor,
(__bridge id)innerColor,
(__bridge id)innerColor,
(__bridge id)outerColor, nil];
maskLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.125],
[NSNumber numberWithFloat:0.875],
[NSNumber numberWithFloat:1.0], nil];
[maskLayer setStartPoint:CGPointMake(0, 0.5)];
[maskLayer setEndPoint:CGPointMake(1, 0.5)];
maskLayer.bounds = self.mainCollectionView.bounds;
maskLayer.anchorPoint = CGPointZero;
[self.mainCollectionView.layer insertSublayer: maskLayer atIndex: 0];
}
}
這將創建一個漂亮的「黑漸變」我的收藏鑑於雙方的影響。可以將更多顏色添加到位置&顏色屬性以優化漸變混合。起點/終點確定梯度的方向和位置。