3
votes

This has been asked before but I didn't find a satisfying answer yet. Is there really no way to set a blendmode for a UIView (that is, use GPU compositing to achieve the blending).

There are some suggestions using CGContextSetBlendMode like here: Draw an image in Additive Blend Mode without using OpenGL but all these solutions I stumbled upon so far seem to use drawing routines and as far as I understand (and as far as my performance measures hint to) this will do the blending on the CPU (and only afterwards upload it to an OpenGL texture for display).

But afaik UIKit uses OpenGL to present the underlying CALayers, so it should be the easiest thing in the world to use separate blending modes right there.

since IOS 5 there also seem to be some particle systems http://www.raywenderlich.com/6063/uikit-particle-systems-in-ios-5-tutorial that also support additive blending (and I doubt that this blending is implemented on the CPU) - so is there maybe by now a way to use additive (or other kinds of) blending when displaying UIViews?

2

2 Answers

2
votes

iOS 7 introduced a new class of Layer called the CABackdropLayer. A Backdrop Layer gets its image from the UIView below it in the hierarchy. This is the layer type Apple uses for its Burn and Overlay effects in the Control Center, Notification Center, Passcode, Dial Pad, etc. as well as the frost/gaussian blur views.

To use this type of layer, subclass UIView like so....

@interface MyBackdropView : UIView
@end

@implementation MyBackdropView
+ (Class)layerClass {
    return [CABackdropLayer class];
}

There are some private API methods to add this capability to existing UIViews too, but they wouldn't be safe for the App Store....

UIView *blendedView = [[UIView alloc] initWithFrame:frame];
[blendedView _setDrawsAsBackdropLayerWithBlendMode:kCGBlendModeColorBurn];
2
votes

You can use compositingFilter:

view.layer.compositingFilter = "colorDodgeBlendMode"

Some other valid values:

"normalBlendMode", "darkenBlendMode","multiplyBlendMode", "colorBurnBlendMode", "lightenBlendMode", "screenBlendMode", "colorDodgeBlendMode", "overlayBlendMode", "softLightBlendMode", "hardLightBlendMode", "differenceBlendMode", "exclusionBlendMode"