iOS/Swift: How to make a blend of multiple UIViews

Viewed 340

I'm trying to make a color dodge blend of an image with other UIView's (CollectionView, a background ImageView, ...). In Android, I can put some view over the others and then apply a BlendMode.COLOR_DODGE or PorterDuffXfermode(PorterDuff.Mode.ADD) to Paint when I'm drawing the bitmap over the canvas.

I tried making this work using the same analogy: putting an UIView over the others and applying the color dodge. I already tried:

override func draw(_ rect: CGRect) {
    self.image?.draw(in: rect, blendMode: .colorDodge, alpha: 1.0)
}

But I noticed that blendMode only works if I draw all the content of view inside of it's draw method and that behavior will not work for me.

There's some way to achieve blending of multiple views in iOS?

1 Answers

You can use compositingFilter:

view.layer.compositingFilter = "colorDodgeBlendMode"

Some other valid values:

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

Related