objective-c - Objective-C - UICollectionView 左侧/右侧的渐变渐变

标签 objective-c gradient uicollectionview

我在 View Controller 的主视图中有一个水平滚动的 UICollectionView,就像这样(灰色是 UIView,木头是 UICollectionView):

我想在此 UICollectionView 的最左边和最右边的 sies 上添加固定的淡化渐变,以便在用户滚动时滚动看起来消失。我该怎么做呢?它是否涉及一些 CAGradientLayer 的使用?如果您能给我任何帮助,我将不胜感激!

最佳答案

感谢 this tutorial at cocoanetics,我实际上设法使用一个 mask 层解决了这个问题.这是我所做的:

@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];
    }
}

这会在我的 Collection View 的两侧创建一个很好的“淡入黑色”效果。可以将更多颜色添加到位置和颜色属性以优化渐变混合。起点/终点决定梯度的方向和位置。

关于objective-c - Objective-C - UICollectionView 左侧/右侧的渐变渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16632001/

相关文章:

ios - 单线程多任务同步

ios - 集成 PayU Money Gateway iOS Swift

python - 如何在 Folium Heat Map 中添加图例/渐变?

ios - 如何仅从 UICollectionView 重新加载更新的单元格

objective-c - 当光标悬停在 NSTextView 内的 NSButton 上时,如何强制光标为 "arrowCursor"?

objective-c - NSTask 不适用于旧版本的 Mac OSX

ios - Core Graphics - 将 CGGradient 与笔画和文本一起使用

jquery - 使用 jQuery 插件进行渐变,添加类以创建悬停效果导致跳转文本

swift - 以编程方式将 collectionView 置于 View 中

swift - UICollectionView didSelectItemAt() 并根据状态添加/删除 View