【发布时间】:2016-09-20 22:28:43
【问题描述】:
不知道在哪里可以找到它,但我想创建一个视图或图层的半透明蒙版,它可以移动、更改不透明度并同时在屏幕上显示多个重叠。
大意是这样的:
两个小盒子可以移动并改变颜色的不透明度。
我尝试过使用CALayer 面具,但这只是穿透,所以我在孔中添加了一个CAShapeLayer,但你可以看到边缘。我尝试在CAShapeLayer 上使用allowsEdgeAntialiasing,但这似乎不起作用。
我见过的其他示例仅适用于 UIImageViews,因为它们使用两个 UIImageViews 相互叠加来获得效果。不幸的是,我需要它来处理任何类型的 UIView。
我也尝试使用CGBlendMode.SourceOut,它效果很好,除了重叠区域更轻,这完全有道理,但不是我想要的。
let blendMode = CGBlendMode.SourceOut
CGContextSaveGState(context)
CGContextSetFillColorWithColor(context, UIColor(white: 0.0, alpha: 0.75).CGColor)
CGContextFillRect(context, rect)
CGContextSetBlendMode(context, blendMode)
CGContextSetFillColorWithColor(context, UIColor(white: 0.0, alpha: 0.25).CGColor)
CGContextFillRect(context, CGRectMake(0, 500, 60, 50))
CGContextSetFillColorWithColor(context, UIColor(white: 0.0, alpha: 1).CGColor)
CGContextFillRect(context, CGRectMake(0, 550, 60, 50))
最后两个填充矩形重叠,第一个是将整个视图设置为具有半透明覆盖。
【问题讨论】:
标签: ios transparency calayer mask