我在 SwiftUI 中使用下面的代碼為所有邊添加了邊框。我想修剪描邊一個特定的位置,如下所示。
想要達到:-

輸出:-

代碼:-
import SwiftUI
struct RoundTrimImage: View {
var body: some View {
VStack{
Button(action: {
print("Round Action")
}) {
Text("Press")
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle()).padding(5)
.overlay(
RoundedRectangle(cornerRadius: 100)
.trim(from: 0, to: CGFloat(0.8))
.stroke(Color.blue, lineWidth: 2)
)
}
}
}
}
問題:有人可以向我解釋如何修剪特定位置,我已經嘗試過上面的代碼,但還沒有結果。
有人可以向我解釋如何獲得進步嗎?
任何幫助將不勝感激。
提前致謝。
uj5u.com熱心網友回復:
可能的方法是使用準備所需的形狀Arc,然后將其旋轉到所需的任何位置。
使用 Xcode 13.2 / iOS 15.2 準備的演示

struct DemoView: View {
var body: some View {
VStack{
Button(action: {
print("Round Action")
}) {
Text("Press")
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle()).padding(5)
.overlay(
RotatedShape(shape: CutShape(), angle: .degrees(-120))
.stroke(Color.blue, lineWidth: 2)
)
}
}
}
}
private struct CutShape: Shape {
func path(in rect: CGRect) -> Path {
Path {
$0.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: rect.midY, startAngle: Angle(degrees: -5), endAngle: Angle(degrees: 5), clockwise: true)
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/405851.html
標籤:
下一篇:為什么單擊按鈕時按鈕會上下移動?
