如何使用swift UI为按钮背景绘制中间带半线的圆?
我想画一个已经在Android端绘制的形状,如下所示:如何使用swift UI为按钮背景绘制中间带半线的圆?,swift,swiftui,Swift,Swiftui,我想画一个已经在Android端绘制的形状,如下所示: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="0dp" android:shape="ring" android:thicknessRatio="2" android:useLevel="false"> <stroke
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false">
<stroke
android:width="2dp"
android:color="@color/color_boader" />
</shape>
有没有更好的方法来实现这一点?您可以使用
路径创建自己的形状
,然后将此形状用作其他视图的背景:
struct按钮带特殊标记:视图{
var body:一些观点{
按钮(操作:{
打印(“已登录”)
}) {
文本(“注册!”)
.background(圆圈半直线()
.中风(
.foregroundColor(.灰色))
}
}
}
结构圆与半线:形状{
func路径(在rect:CGRect中)->path{
var path=path()
让半径=rect.size.width/2
path.addArc(中心:CGPoint(x:radius,y:radius/2),半径:radius,startAngle:.degrees(0),endAngle:.degrees(360),顺时针:false)
addLine(to:CGPoint(x:radius,y:radius/2))//您可以使用radius来适应文本
返回路径
}
}
您可以稍微使用偏移或使用半径
来更好地居中。我只是在很短的时间内取得了这个结果,但你可以更进一步:
ZStack(alignment: .trailing) {
Spacer()
.frame(width: 40, height: 2)
.background(Color.boader)
Text("Sign up")
.foregroundColor(.white)
.padding()
.padding()
.overlay(
Circle()
.stroke(Color.boader, lineWidth: 2))
.offset(x: 20)
}