gpt4 book ai didi

ios - Swift - 如何使用 UIGraphics 混合模式?

转载 作者:行者123 更新时间:2023-12-01 16:06:18 25 4
gpt4 key购买 nike

我想为 AppleWatch 开发一种进度条动画。
我决定用 来做这一切UIGraphicContext .

由于我是一个完整的初学者,我真的不明白如何将某种“globalCompositeOperation”应用于我的上下文。

为了更好地说明我的想法,这里有一些图片:

Image #1

Image #2

Image #3

到目前为止,这是我的源代码:

let size = CGSize(width: 300, height: 100)
UIGraphicsBeginImageContext(size)

let context = UIGraphicsGetCurrentContext()!

context.setFillColor(UIColor.red.cgColor)
context.fill(CGRect(x: 0.0, y: 0.0, width: size.width * 0.5, height: size.height))
context.setBlendMode(CGBlendMode.destinationOver)

let count = 5;
let padding = (size.width / CGFloat(count)) * 0.5
for i in 0...count {
let offsetX = size.width * (CGFloat(i) / CGFloat(count))
let rect = CGRect(x: offsetX + padding/2, y: 0, width: padding, height: size.height)
context.setFillColor(UIColor.green.cgColor)
context.fill(rect)
}
UIGraphicsEndImageContext()

我想这是一种错误的方法,因为结果更像是这样:

Image #4

任何帮助将非常感激。提前致谢。

最佳答案

你要.sourceAtop .这是一个演示:

    let green = UIImage(named:"green.png")! // your original 5 green rects
let sz = green.size
let red = UIGraphicsImageRenderer(size:sz).image {
ctx in
UIColor.red.setFill()
// width value determines how much we will fill
ctx.fill(CGRect(x: 0, y: 0, width: sz.width/2, height: sz.height))
}
let result = UIGraphicsImageRenderer(size:sz).image {
ctx in
green.draw(at: .zero)
red.draw(at: .zero, blendMode: .sourceAtop, alpha: 1)
}

enter image description here

只需更改 width: sz.width/2 处的值即可对于不同的“温度计”量。

关于ios - Swift - 如何使用 UIGraphics 混合模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59909716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com