Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用swift UI为按钮背景绘制中间带半线的圆?_Swift_Swiftui - Fatal编程技术网

如何使用swift UI为按钮背景绘制中间带半线的圆?

如何使用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

我想画一个已经在Android端绘制的形状,如下所示:

<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)
                    }