是否可以根据图像形状创建精确的拍子区域?

问题描述 投票:0回答:1

我正在尝试制作一张互动式美国地图,但要使其正常工作,每个州的灾区将需要根据其形状进行精确定位。否则,用户将触发其他邻国。

附上代码。我已经搜寻了一切,却一无所获。任何的意见都将会有帮助。谢谢。

struct MapGraphic:视图{

@State private var stateColor:Color = .white

var body: some View {

    ZStack {

        Image("California")
        .resizable()
        .scaledToFit()
        .colorMultiply(Color("lGrey"))
        .frame(width: 50.45, height: 87.41)

        .onTapGesture {

        switch self.stateColor {
        case .white:
            self.stateColor = .blue
        case .blue:
            self.stateColor = .red
        default:
            self.stateColor = .white
        }

        }.colorMultiply(stateColor)

    }

}

}

swift image swiftui gesture tap
1个回答
0
投票

您需要两件事:

1)构造图像矩形的路径(逐点)

extension Path : Shape {

    /// Describes this shape as a path within a rectangular frame of reference.
    ///
    /// - Parameter rect: The frame of reference for describing this shape.
    /// - Returns: A path that describes this shape.
    public func path(in _: CGRect) -> Path

2)将该路径(形状)设置为图像的命中测试区域

/// Returns a new view that defines the content shape for
/// hit-testing `self` as `shape`. `eoFill` defines whether the
/// shape is interpreted using the even-odd winding number rule or
/// not.
@inlinable public func contentShape<S>(_ shape: S, eoFill: Bool = false) -> some View where S : Shape
© www.soinside.com 2019 - 2024. All rights reserved.