【问题标题】:Button Stroke Set Gap In specific position SwiftuiButton Stroke Set Gap In specific position Swiftui
【发布时间】:2022-01-06 16:28:38
【问题描述】:

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

问题:谁能给我解释一下如何修剪特定位置,我已经尝试过上面的代码,但还没有结果。

有人可以向我解释如何获得进度吗?

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: button swiftui uibutton stroke cornerradius


    【解决方案1】:

    可能的方法是使用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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 1970-01-01
      • 2021-05-14
      • 2022-12-27
      • 2022-12-01
      • 2022-12-01
      • 2022-12-02
      • 2015-09-26
      • 2022-01-22
      相关资源
      最近更新 更多