gpt4 book ai didi

uiscrollview - 将 UIScrollView 的底部淡化为透明

转载 作者:行者123 更新时间:2023-12-04 04:00:14 25 4
gpt4 key购买 nike

我有一个 UIScrollView,我需要底部淡入透明,这样它就不会突然切断内容。 UIScrollView 的背景是自定义颜色。这是我到目前为止所拥有的,但图层显示为白色,而不是自定义颜色。这就是我想要的,但仅限于底部。

:This is what I am going for, but only on the bottom

这是我到目前为止所拥有的:

maskLayer = [CAGradientLayer layer];

CGColorRef firstColor = [UIColor colorWithRed:141 green:211 blue:244 alpha:1.0].CGColor;
CGColorRef secondColor = [UIColor colorWithRed:141 green:211 blue:244 alpha:0.8].CGColor;
CGColorRef thirdColor = [UIColor colorWithRed:141 green:211 blue:244 alpha:0.2].CGColor;
CGColorRef fourthColor = [UIColor colorWithRed:141 green:211 blue:244 alpha:0.0].CGColor;

maskLayer.colors = [NSArray arrayWithObjects:(__bridge id)firstColor, (__bridge id)secondColor, (__bridge id)thirdColor, (__bridge id)fourthColor, nil];
maskLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.2],
[NSNumber numberWithFloat:0.8],
[NSNumber numberWithFloat:1.0], nil];
maskLayer.frame = CGRectMake(0, 285, self.loginScrollView.frame.size.width, 40);
maskLayer.anchorPoint = CGPointZero;
[self.loginScrollView.layer addSublayer:maskLayer];

出于某种原因,该层显示为白色,并且只有 scrollView 宽度的一半。

最佳答案

我使用的方法是子类化 UIScrollView ,并在layoutSubviews中创建 mask 层方法。

这是我的代码,它淡化 UIScrollView 的顶部和底部从背景颜色到透明:

#import "FadingScrollView.h"
#import <QuartzCore/QuartzCore.h>

static float const fadePercentage = 0.2;

@implementation FadingScrollView

// ...

- (void)layoutSubviews
{
[super layoutSubviews];

NSObject * transparent = (NSObject *) [[UIColor colorWithWhite:0 alpha:0] CGColor];
NSObject * opaque = (NSObject *) [[UIColor colorWithWhite:0 alpha:1] CGColor];

CALayer * maskLayer = [CALayer layer];
maskLayer.frame = self.bounds;

CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(self.bounds.origin.x, 0,
self.bounds.size.width, self.bounds.size.height);

gradientLayer.colors = [NSArray arrayWithObjects: transparent, opaque,
opaque, transparent, nil];

// Set percentage of scrollview that fades at top & bottom
gradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:fadePercentage],
[NSNumber numberWithFloat:1.0 - fadePercentage],
[NSNumber numberWithFloat:1], nil];

[maskLayer addSublayer:gradientLayer];
self.layer.mask = maskLayer;
}

@end

如果您只想淡化底部,请更改此行:
// Fade bottom of scrollview only
gradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:1.0 - fadePercentage],
[NSNumber numberWithFloat:1], nil];

当我自己实现这个时,我发现 this SO question有帮助,和 this gist在github上。

编辑:我已将此代码放在 github 上,请参阅 here .

关于uiscrollview - 将 UIScrollView 的底部淡化为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com