gpt4 book ai didi

swift - 如何在 SwiftUI 中将一个形状与另一个形状一起剪辑?

转载 作者:行者123 更新时间:2023-12-03 08:23:36 25 4
gpt4 key购买 nike

我想用绿色半圆剪辑红色方 block 。我尝试了 .mask 和 .clipShape 但是我无法让它发挥作用。有人可以分享一下它是如何完成的吗?

import SwiftUI

struct TestEndBarView: View {
var body: some View {
ZStack {
Rectangle()
.fill(Color.red)
.frame(width: 500, height: 500)
Circle()
.trim(from: 0.5, to: 1.0)
.fill(Color.green)
.rotationEffect(Angle(degrees: 90))
.frame(width: 500, height: 500)
.offset(x: -250)

}
}
}

struct TestEndBarView_Previews: PreviewProvider {
static var previews: some View {
TestEndBarView()
}
}

A red square with a green half circle

最佳答案

使用eoFill,如代码中所示:


enter image description here


struct ContentView: View {

var body: some View {

CustomShape()
.fill(Color.purple, style: FillStyle(eoFill: true, antialiased: true))
.frame(width: 200, height: 200, alignment: .center)
.shadow(radius: 10.0)

}
}

struct CustomShape: Shape {

func path(in rect: CGRect) -> Path {

return Path { path in
path.addArc(center: CGPoint(x: rect.minX, y: rect.midY), radius: rect.height/2, startAngle: Angle(degrees: 90), endAngle: Angle(degrees: 270), clockwise: true)
path.addRect(CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: rect.height))

}

}
}

关于swift - 如何在 SwiftUI 中将一个形状与另一个形状一起剪辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67127570/

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