分享
三行代码  ›  专栏  ›  技术社区  ›  Niko Gamulin

如何改变形状颜色点击

  •  1
  • Niko Gamulin  · 技术社区  · 1 周前

    我想实现我想改变形状的颜色。我试图修改 example for changing the shape on tap

    import SwiftUI
    
    struct PathView: View {
        @State private var insetAmount: CGFloat = 50
        @State private var fillColor: UIColor = UIColor.white
        var body: some View {
            GeometryReader {
                geometry in
                ZStack {
                    Trapezoid(insetAmount: insetAmount, fillColor: fillColor)
                                .frame(width: 200, height: 100)
                                .onTapGesture {
                                    self.insetAmount = CGFloat.random(in: 10...90)
                                    let demoColors = [UIColor.blue, UIColor.green, UIColor.red]
                                    self.fillColor = demoColors.randomElement() ?? UIColor.white
                                }
                }.frame(width: geometry.size.width, height: geometry.size.height, alignment: .topLeading)
                
            }
        }
    }
    
    struct Trapezoid: Shape {
        var insetAmount: CGFloat
        var fillColor: UIColor
    
        func path(in rect: CGRect) -> Path {
            var path = Path()
    
            path.move(to: CGPoint(x: 0, y: rect.maxY))
            path.addLine(to: CGPoint(x: insetAmount, y: rect.minY))
            path.addLine(to: CGPoint(x: rect.maxX - insetAmount, y: rect.minY))
            path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
            path.addLine(to: CGPoint(x: 0, y: rect.maxY))
            path.closeSubpath()
            fillColor.setFill()
            UIColor.white.setStroke()
            path.fill()
            return path
       }
    }
    

    然而,这条路是黑色的路径.填充()显示警告:

    调用“fill(style:)”的结果未使用

    有人知道如何设置形状的颜色属性并在点击时改变它吗?

    1 回复  |  直到 1 周前
        1
  •  1
  •   Asperi    1 周前

    fill 应应用于 Shape .

    下面是可能的解决方案(用Xcode 12测试)

    struct PathView: View {
        @State private var insetAmount: CGFloat = 50
        @State private var fillColor: UIColor = UIColor.white
        var body: some View {
            GeometryReader {
                geometry in
                ZStack {
                    Trapezoid(insetAmount: insetAmount)
                                .fill(Color(fillColor))           // << here !!
                                .frame(width: 200, height: 100)
                                .onTapGesture {
                                    self.insetAmount = CGFloat.random(in: 10...90)
                                    let demoColors = [UIColor.blue, UIColor.green, UIColor.red]
                                    self.fillColor = demoColors.randomElement() ?? UIColor.white
                                }
                }.frame(width: geometry.size.width, height: geometry.size.height, alignment: .topLeading)
    
            }
        }
    }
    
    struct Trapezoid: Shape {
        var insetAmount: CGFloat
    
        func path(in rect: CGRect) -> Path {
            var path = Path()
    
            path.move(to: CGPoint(x: 0, y: rect.maxY))
            path.addLine(to: CGPoint(x: insetAmount, y: rect.minY))
            path.addLine(to: CGPoint(x: rect.maxX - insetAmount, y: rect.minY))
            path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
            path.addLine(to: CGPoint(x: 0, y: rect.maxY))
            path.closeSubpath()
            return path
       }
    }