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

标签 iphone uiview core-graphics

我们的主 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/

相关文章:

iphone - UITableViewCell 上的 UIPopoverController

ios - 返回错误的设备方向

ios - 子类化自定义CollectionView Cell不显示父UI

ios - 如何找到 UILabel 的现有 UIFontTextStyle?

iphone - 发送带有帐户信息的图片,例如 accountID(ios)

ios - 如何更改 UIPageViewController 中页面指示器的背景颜色

ios - 如何在 UIView 上设置平滑的边框颜色?

ios - 使用 CoreGraphics 在 iPhone 上绘制简单的线条

iphone - 沿着 Iphone 中的路径拖动

ios - 从 -drawRewct : using CoreGraphics 调用回调方法