与您链接到的 Cocoanetics 帖子中所做的类似,您可以创建一个CAGradientLayer
来覆盖您的滚动视图。使用滚动视图的背景颜色(在我的示例中为白色)使其向左、右、上和下边缘淡出:
CGColorRef innerColor = [UIColor colorWithWhite:1.0 alpha:0.0].CGColor;
CGColorRef outerColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
// first, define a horizontal gradient (left/right edges)
CAGradientLayer* hMaskLayer = [CAGradientLayer layer];
hMaskLayer.opacity = .7;
hMaskLayer.colors = [NSArray arrayWithObjects:(id)outerColor,
(id)innerColor, (id)innerColor, (id)outerColor, nil];
hMaskLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.15],
[NSNumber numberWithFloat:0.85],
[NSNumber numberWithFloat:1.0], nil];
hMaskLayer.startPoint = CGPointMake(0, 0.5);
hMaskLayer.endPoint = CGPointMake(1.0, 0.5);
hMaskLayer.bounds = self.scrollView.bounds;
hMaskLayer.anchorPoint = CGPointZero;
CAGradientLayer* vMaskLayer = [CAGradientLayer layer];
// without specifying startPoint and endPoint, we get a vertical gradient
vMaskLayer.opacity = hMaskLayer.opacity;
vMaskLayer.colors = hMaskLayer.colors;
vMaskLayer.locations = hMaskLayer.locations;
vMaskLayer.bounds = self.scrollView.bounds;
vMaskLayer.anchorPoint = CGPointZero;
// you must add the masks to the root view, not the scrollView, otherwise
// the masks will move as the user scrolls!
[self.view.layer addSublayer: hMaskLayer];
[self.view.layer addSublayer: vMaskLayer];
免责声明:这确实使四个角的渐变/淡化加倍。您可以查看结果并确定它们是否适合您。如果没有,您也可以尝试在 Photoshop 中绘制透明图像,并UIImageView
在顶部添加一个子视图作为蒙版,使用您绘制的图像。
Youtube 屏幕截图