正如标题所示,我试图给予一些UI
ImageViews的淡出效果,因为它们越来越接近我的UIScrollView的四个边缘.由于用户可以拖动UIImage,如果他们将它们拖向边缘,它们就会开始淡出,而不是像UIScrollView的边框那样给出那样的剪切效果.我试过这个教程:
http://www.cocoanetics.com/2011/08/adding-fading-gradients-to-uitableview/
建议在另一个堆栈溢出问题,但它只能应用于UITableViews.我希望图像在距离边界1厘米处开始衰落.
解决方法
类似于您链接到的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)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子视图作为蒙版添加.