SwiftUI 圆形描边渐变

标签 swift user-interface swiftui

我已经实现了一个圆形笔划,如下图所示(当然没有数字😉)。

不幸的是,我遇到了两个您可以帮助我的问题。

  • 我想要 渐变沿着这条线走,即这张图片中最暗的点应该总是在笔画的末尾(1b)而不是现在在(1a)
  • 是否可以给中风一个 lineCap .round一侧和一条线帽.butt另一方面?我想在 (1b) 处有一条圆线,在 (2) 处有一条直线,它对应于 .butt。

  • 这是我的示例代码

    struct CircularGradientLine: View {
        private let gradient = LinearGradient(
            gradient: Gradient(colors: [StyleGuide.Color.primary, .white]),
            startPoint: .leading,
            endPoint: .trailing)
        public var body: some View {
            ZStack {
                Circle()
                    .stroke(Color.white, lineWidth: 46)
    
                Circle()
                    .trim(from: 0, to: CGFloat(0.8))
                    .stroke(gradient, style:
                        StrokeStyle(lineWidth: 46,
                                    lineCap: .round))
            }.padding(60)
        }
    }
    

    如果有人能在那里帮助我,我会很高兴😇

    enter image description here

    最佳答案

    1)你可以尝试:

    private let gradient = AngularGradient(
        gradient: Gradient(colors: [Color.blue, .white]),
        center: .center,
        startAngle: .degrees(270),
        endAngle: .degrees(0))
    

    2)你可以尝试这样的事情:
    public var body: some View {
        ZStack {
            Circle().stroke(Color.white, lineWidth: 46)
    
            Circle()
                .trim(from: 0, to: CGFloat(0.8))
                .stroke(gradient, style: StrokeStyle(lineWidth: 46, lineCap: .round))
                .overlay(
                    Circle().trim(from: 0, to: CGFloat(0.8))
                    .rotation(Angle.degrees(-4))
                    .stroke(gradient, style: StrokeStyle(lineWidth: 46, lineCap: .butt)))
    
        }.padding(60)
    }
    

    关于SwiftUI 圆形描边渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62427612/

    相关文章:

    c# - 为什么 Thread.Sleep() 会卡住表单?

    ios - 将 BindableObject 传递给 View

    ios - Firebase 如何检查快照的子级本身是否属于 Datasnapshot 类型

    android - swift/objective c 中是否有相当于 getServingUrl() 的方法?

    python - 向图中添加导航工具栏(matplotlib 和 PyQt4)

    swiftui - 在 SwiftUI 中从 child 的 child 调用 parent 的功能

    view - SwiftUI:如何将 View 与 HStack 的 subview 对齐?

    ios - 从共享扩展中检索 imageURL

    ios - Fabric 请求不在存档或 iTunes Connect dSYM 文件中的 dSYM

    java - TextView 不使用 Html.fromHtml 应用 HTML 样式