如何使用 SwiftUI 绘制弧线? [英] How to draw an arc with SwiftUI?

查看:44
本文介绍了如何使用 SwiftUI 绘制弧线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 SwiftUI 画一个弧形.我正在寻找类似用于 Circle() 的段修饰符之类的东西,但我找不到.我应该可以设置开始和结束角度.

解决方案

你真的应该检查一下:

import SwiftUI结构内容视图:查看{var主体:一些视图{我的形状()}}结构我的形状:形状{func path(in rect: CGRect) ->小路 {var p = 路径()p.addArc(center: CGPoint(x: 100, y:100), radius: 50, startAngle: .degrees(0), endAngle: .degrees(90), 顺时针: true)return p.strokedPath(.init(lineWidth: 3, dash: [5, 3], dashPhase: 10))}}

I'd like to draw an arc shape with SwiftUI. I was looking for something like a segment modifier to use on Circle(), but I couldn't find one. I should be able to set a start and end angle.

解决方案

You should really check this: https://developer.apple.com/tutorials/swiftui/drawing-paths-and-shapes

And here's a shortcut:

import SwiftUI

struct ContentView : View {
    var body: some View {
        MyShape()
    }
}

struct MyShape : Shape {
    func path(in rect: CGRect) -> Path {
        var p = Path()

        p.addArc(center: CGPoint(x: 100, y:100), radius: 50, startAngle: .degrees(0), endAngle: .degrees(90), clockwise: true)

        return p.strokedPath(.init(lineWidth: 3, dash: [5, 3], dashPhase: 10))
    }    
}

这篇关于如何使用 SwiftUI 绘制弧线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆