How to use CGBlendMode in a UIView that scrolls over a fixed background?

Our main UIView is a fixed background UIScrollView (very common, obviously). In this scrollView, we have several UIViews that contain content and scroll up and down as the user scrolls (also general). Each of the UIViews has its own background, a simple gradient from white to black.

The goal is for the background gradient of these (internal) UIViews to be partially opaque and use a CGBlendMode other than "kCGBlendModeNormal" (specifically, "kCGBlendModeOverlay"). You should be able to see the “parent” scrollViews fixed background when UIViews scroll up and down above it.

- (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, . blendMode UIView , , , . , SINGLE UIView; blendMode "". scrollView ( ), , .

, : http://img2.sbck.us/blendmode.png

!

+3
3

, , , . iOS , , . .

+4

, , . . drawRect :

  • .
  • .
  • .
  • .
  • .
  • .
  • .
  • .

.

+2

, , 'compositingFilter' CALayer . CALayer.h " , (, ) . - nil, ".

, CoreImage, , () iOS.

, - OpenGL. UIView OpenGL , UIView , .

0

Source: https://habr.com/ru/post/1789106/


All Articles