在继承自UIScrollView的类的layoutSubViews中设置
- (void)layoutSubviews {
[super layoutSubviews];
NSObject *transparent = (NSObject*) [[UIColor colorWithWhite:0 alpha:0] CGColor];
NSObject *opaque = (NSObject*) [[UIColorcolorWithWhite:0 alpha:1] CGColor];
CALayer *maskLayer = [CALayer layer];
maskLayer.frame=self.bounds;
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame= maskLayer.bounds;
gradientLayer.colors=@[transparent, transparent, opaque, opaque, transparent, transparent];
CGFloatpercent1 =2.0/self.bounds.size.width;
CGFloatpercent2 =12.0/self.bounds.size.width;
gradientLayer.locations=@[@(0),@(percent1),@(percent2),@(1- percent2),@(1- percent1),@(1)];
gradientLayer.startPoint=CGPointMake(0,0.5);
gradientLayer.endPoint=CGPointMake(1,0.5);
[maskLayer addSublayer:gradientLayer];
self.layer.mask= maskLayer;
}