gpt4 book ai didi

iphone - 如何在滚动到固定背景上方的 UIView 上使用 CGBlendMode?

转载 作者:行者123 更新时间:2023-12-03 19:37:18 24 4
gpt4 key购买 nike

我们的主 UIView 是一个具有固定背景图像的 UIScrollView(显然很常见)。在该 ScrollView 中,我们有几个 UIView,它们保存内容并随着用户滚动而上下滚动(也很常见)。这些 UIView 每个都有自己的背景,从白色到黑色的简单渐变。

目标是让这些(内部)UIView 的背景渐变部分不透明,并使用“kCGBlendModeNormal”以外的 CGBlendMode(特别是“kCGBlendModeOverlay”)。当 UIView 在其上方上下滚动时,您应该能够看到“父”scrollView 的固定背景图像。

- (void)drawRect:(CGRect)rect {
gradientStart = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
gradientEnd = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[2] = { 0.0f, 1.0f };
NSArray *colors = [NSArray arrayWithObjects:(id)gradientStart.CGColor, (id)gradientEnd.CGColor, nil];

CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations);
CGColorSpaceRelease(colorSpace);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetAlpha(context, 0.50); //this works!
CGContextSetBlendMode(context, kCGBlendModeOverlay); //doesn’t seem to do anything!

CGContextClearRect(context, rect);

CGPoint startPoint, endPoint;
startPoint.x = 0.0;
startPoint.y = 0.0;
endPoint.x = 0.0;
endPoint.y = rect.size.height;

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

CGGradientRelease(gradient);

[super drawRect:rect];
}

除了 CGContextSetBlendMode 被忽略之外,一切都按预期工作。我们似乎找不到一种方法来改变 UIView 相对于其背后的混合模式,就像使用 alpha 一样。请注意,这与在单个 UIView 中构建多个层不同;在这种情况下,此技术确实会更改“顶部”图层的混合模式。我们希望看到父scrollView的固定背景图像(当我们在其上方上下滚动 subview 时),并应用alpha和覆盖混合。

这是显示问题的图像:http://img2.sbck.us/blendmode.png

预先感谢您的帮助!

最佳答案

我相信您当前的设置无法实现您想要的。在 iOS 上, View 的混合模式根本不可能对 View 下绘制的内容产生影响。您必须在同一 View 中绘制 ScrollView 的背景和渐变。

关于iphone - 如何在滚动到固定背景上方的 UIView 上使用 CGBlendMode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4856896/

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