Ios 如何在普通UIView(非UIImage)上获得多重混合模式
我的iPad应用程序中有一张图片,我基本上想在上面放置一个滤色器。对于这一点,我有一个彩色的UIView,它被遮罩成一个特定的形状,我把它覆盖在图像上。因为调整alpha并没有给我想要的效果,所以我想使用混合模式 据我所知,您只能在图像上使用混合模式,而不能在普通视图上使用混合模式。视图的颜色是动态的,所以我不能只使用图片 我还尝试将视图栅格化为一幅图像,但结果都是像素和奇数之类的,但可能是我做错了什么Ios 如何在普通UIView(非UIImage)上获得多重混合模式,ios,uiview,filter,uiimage,blend,Ios,Uiview,Filter,Uiimage,Blend,我的iPad应用程序中有一张图片,我基本上想在上面放置一个滤色器。对于这一点,我有一个彩色的UIView,它被遮罩成一个特定的形状,我把它覆盖在图像上。因为调整alpha并没有给我想要的效果,所以我想使用混合模式 据我所知,您只能在图像上使用混合模式,而不能在普通视图上使用混合模式。视图的颜色是动态的,所以我不能只使用图片 我还尝试将视图栅格化为一幅图像,但结果都是像素和奇数之类的,但可能是我做错了什么 因此,基本问题是:是否可以将混合模式应用于视图?还是我应该采取完全不同的方法来达到相同的目标
因此,基本问题是:是否可以将混合模式应用于视图?还是我应该采取完全不同的方法来达到相同的目标?看看CALayer的合成过滤器文档: 在颜色覆盖视图中,您可以将
view.layer.compositingFilter
设置为a,以实现与其后面内容的混合。下面是一些应用了多重混合模式的示例代码(用您自己的图像替换UIImage(名为:“”)
进行测试)
iOS 13,Swift 5 关键是在相应的CALayer上设置
compositingFilter
属性(顶部的一个-它与后面的任何内容混合)。颜色混合在UIView或Calayer之间工作。你可以在这里找到混合模式。要使用过滤器,只需删除CI
&名称的第一个字母小写(例如CIDivideBlendMode
变为divideBlendMode
)。下面是一些游乐场代码来说明:
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 900))
PlaygroundPage.current.liveView = view
let heightIncrement = view.frame.height / 13
let widthIncrement = view.frame.width / 7
// TOP EXAMPLE (UIViews)
let backgroundView = UIView(frame: CGRect(x: widthIncrement * 2,
y: heightIncrement,
width: widthIncrement * 3,
height: heightIncrement * 5))
backgroundView.backgroundColor = .black
view.addSubview(backgroundView)
let overlayView1 = UIView(frame: CGRect(x: widthIncrement,
y: heightIncrement * 2,
width: widthIncrement * 5,
height: heightIncrement))
overlayView1.backgroundColor = UIColor.yellow.withAlphaComponent(0.3)
overlayView1.layer.compositingFilter = "darkenBlendMode"
view.addSubview(overlayView1)
let overlayView2 = UIView(frame: CGRect(x: widthIncrement,
y: heightIncrement * 4,
width: widthIncrement * 5,
height: heightIncrement))
overlayView2.backgroundColor = UIColor.yellow.withAlphaComponent(0.3)
overlayView2.layer.compositingFilter = "divideBlendMode"
view.addSubview(overlayView2)
// BOTTOM EXAMPLE (CALayers)
let backgroundLayer = CALayer()
backgroundLayer.frame = CGRect(x: widthIncrement * 2,
y: heightIncrement * 7,
width: widthIncrement * 3,
height: heightIncrement * 5)
backgroundLayer.backgroundColor = UIColor.black.cgColor
view.layer.addSublayer(backgroundLayer)
let overlayLayer1 = CALayer()
overlayLayer1.frame = CGRect(x: widthIncrement,
y: heightIncrement * 8,
width: widthIncrement * 5,
height: heightIncrement)
overlayLayer1.backgroundColor = UIColor.yellow.withAlphaComponent(0.3).cgColor
overlayLayer1.compositingFilter = "darkenBlendMode"
view.layer.addSublayer(overlayLayer1)
let overlayLayer2 = CALayer()
overlayLayer2.frame = CGRect(x: widthIncrement,
y: heightIncrement * 10,
width: widthIncrement * 5,
height: heightIncrement)
overlayLayer2.backgroundColor = UIColor.yellow.withAlphaComponent(0.3).cgColor
overlayLayer2.compositingFilter = "divideBlendMode"
view.layer.addSublayer(overlayLayer2)
结果如下所示:
我证实了同样的代码在iOS中也能工作。以下是视图控制器代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let heightIncrement = view.frame.height / 13
let widthIncrement = view.frame.width / 7
// TOP EXAMPLE (UIViews)
let backgroundView = UIView(frame: CGRect(x: widthIncrement * 2,
y: heightIncrement,
width: widthIncrement * 3,
height: heightIncrement * 5))
backgroundView.backgroundColor = .black
view.addSubview(backgroundView)
let overlayView1 = UIView(frame: CGRect(x: widthIncrement,
y: heightIncrement * 2,
width: widthIncrement * 5,
height: heightIncrement))
overlayView1.backgroundColor = UIColor.yellow.withAlphaComponent(0.3)
overlayView1.layer.compositingFilter = "darkenBlendMode"
view.addSubview(overlayView1)
let overlayView2 = UIView(frame: CGRect(x: widthIncrement,
y: heightIncrement * 4,
width: widthIncrement * 5,
height: heightIncrement))
overlayView2.backgroundColor = UIColor.yellow.withAlphaComponent(0.3)
overlayView2.layer.compositingFilter = "divideBlendMode"
view.addSubview(overlayView2)
// BOTTOM EXAMPLE (CALayers)
let backgroundLayer = CALayer()
backgroundLayer.frame = CGRect(x: widthIncrement * 2,
y: heightIncrement * 7,
width: widthIncrement * 3,
height: heightIncrement * 5)
backgroundLayer.backgroundColor = UIColor.black.cgColor
view.layer.addSublayer(backgroundLayer)
let overlayLayer1 = CALayer()
overlayLayer1.frame = CGRect(x: widthIncrement,
y: heightIncrement * 8,
width: widthIncrement * 5,
height: heightIncrement)
overlayLayer1.backgroundColor = UIColor.yellow.withAlphaComponent(0.3).cgColor
overlayLayer1.compositingFilter = "darkenBlendMode"
view.layer.addSublayer(overlayLayer1)
let overlayLayer2 = CALayer()
overlayLayer2.frame = CGRect(x: widthIncrement,
y: heightIncrement * 10,
width: widthIncrement * 5,
height: heightIncrement)
overlayLayer2.backgroundColor = UIColor.yellow.withAlphaComponent(0.3).cgColor
overlayLayer2.compositingFilter = "divideBlendMode"
view.layer.addSublayer(overlayLayer2)
}
}
下面是模拟器中的结果:
和?1) 该链接是关于使用图层的。在您的回答中,您将“层”替换为“视图”,它当然不起作用。2) 即使我使用了他们的建议-从您的链接:“iOS中的层不支持此属性”。。。DownvotedUIViews有图层我刚刚在iPhone 6上运行了一个测试应用程序,它使用了
overlay.layer.compositingFilter=“multiplyBlendMode”
,效果非常好!我看不到iOS支持问题。此解决方案不再有效!在iOS 13中,设置CALayer.compositingFilter是不可操作的。还有其他人遇到此问题吗?(2019年9月)谢谢!请您解释一下,您是如何发现可以在compositingFilter属性上使用像“divideBlendMode”这样的字符串,而不是像“CIDivideBlendMode”这样的字符串的?在文档中有compositingFilter:Any?
,我不清楚如何使用它
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let heightIncrement = view.frame.height / 13
let widthIncrement = view.frame.width / 7
// TOP EXAMPLE (UIViews)
let backgroundView = UIView(frame: CGRect(x: widthIncrement * 2,
y: heightIncrement,
width: widthIncrement * 3,
height: heightIncrement * 5))
backgroundView.backgroundColor = .black
view.addSubview(backgroundView)
let overlayView1 = UIView(frame: CGRect(x: widthIncrement,
y: heightIncrement * 2,
width: widthIncrement * 5,
height: heightIncrement))
overlayView1.backgroundColor = UIColor.yellow.withAlphaComponent(0.3)
overlayView1.layer.compositingFilter = "darkenBlendMode"
view.addSubview(overlayView1)
let overlayView2 = UIView(frame: CGRect(x: widthIncrement,
y: heightIncrement * 4,
width: widthIncrement * 5,
height: heightIncrement))
overlayView2.backgroundColor = UIColor.yellow.withAlphaComponent(0.3)
overlayView2.layer.compositingFilter = "divideBlendMode"
view.addSubview(overlayView2)
// BOTTOM EXAMPLE (CALayers)
let backgroundLayer = CALayer()
backgroundLayer.frame = CGRect(x: widthIncrement * 2,
y: heightIncrement * 7,
width: widthIncrement * 3,
height: heightIncrement * 5)
backgroundLayer.backgroundColor = UIColor.black.cgColor
view.layer.addSublayer(backgroundLayer)
let overlayLayer1 = CALayer()
overlayLayer1.frame = CGRect(x: widthIncrement,
y: heightIncrement * 8,
width: widthIncrement * 5,
height: heightIncrement)
overlayLayer1.backgroundColor = UIColor.yellow.withAlphaComponent(0.3).cgColor
overlayLayer1.compositingFilter = "darkenBlendMode"
view.layer.addSublayer(overlayLayer1)
let overlayLayer2 = CALayer()
overlayLayer2.frame = CGRect(x: widthIncrement,
y: heightIncrement * 10,
width: widthIncrement * 5,
height: heightIncrement)
overlayLayer2.backgroundColor = UIColor.yellow.withAlphaComponent(0.3).cgColor
overlayLayer2.compositingFilter = "divideBlendMode"
view.layer.addSublayer(overlayLayer2)
}
}