简体   繁体   English

iOS/Swift:如何混合多个 UIView

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

I'm trying to make a color dodge blend of an image with other UIView's (CollectionView, a background ImageView, ...).我正在尝试将图像与其他 UIView(CollectionView、背景 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.在 Android 中,当我在画布上绘制位图时,我可以将一些视图放在其他视图上,然后将BlendMode.COLOR_DODGEPorterDuffXfermode(PorterDuff.Mode.ADD)应用于 Paint。

I tried making this work using the same analogy: putting an UIView over the others and applying the color dodge.我尝试使用相同的类比来完成这项工作:将 UIView 放在其他视图上并应用颜色减淡。 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.但是我注意到 blendMode 只有在我在它的 draw 方法中绘制视图的所有内容时才有效,并且这种行为对我不起作用。

There's some way to achieve blending of multiple views in iOS?有什么方法可以在 iOS 中实现多个视图的混合?

You can use compositingFilter :您可以使用compositingFilter

view.layer.compositingFilter = "colorDodgeBlendMode"

Some other valid values:其他一些有效值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM