【问题标题】:How to display an image where the user taps? SwiftUI如何在用户点击的地方显示图像? SwiftUI
【发布时间】:2020-04-20 23:36:35
【问题描述】:

我正在制作一个采矿攻丝游戏,我想在用户点击的任何地方显示一个锤子。

我的意思是,无论用户点击哪里,锤子图像都会停留一秒钟。

有办法吗?

我的示例代码如下:

struct Level1: View {

@State var tapScore = 0
@State var showingMinedHammer = false

func showMinedHammer() {
    self.showingMinedHammer = true
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
    self.showingMinedHammer = false
    }
}

func mine() {
    tapScore += 1
    showMinedHammer()
}

var body: some View {
     GeometryReader { geometryProxy in
        ZStack {
Image("mine1").resizable().frame(width: UIScreen.main.bounds.height * 1.4, height: UIScreen.main.bounds.height)
              .onTapGesture {
            self.mine()
            }

if self.showingMinedHammer {
                Image(systemName: "hammer.fill")
                .resizable()
                .frame(width: 30, height: 30)
            }

}
}.edgesIgnoringSafeArea(.all)
}
}

【问题讨论】:

标签: image position swiftui


【解决方案1】:

它只需要读取点击位置并将其用作锤子图像的位置,如下所示 - 全部由 SwiftUI 完成

使用 Xcode 11.4 / iOS 13.4 测试

这里只修改了一部分

@State private var location = CGPoint.zero      // < here !!
var body: some View {
    GeometryReader { geometryProxy in
        ZStack {
            Image("mine1").resizable().frame(width: UIScreen.main.bounds.height * 1.4, height: UIScreen.main.bounds.height)
                .gesture(DragGesture(minimumDistance: 0).onEnded { value in
                    self.location = value.location // < here !!
                    self.mine()
                })
            if self.showingMinedHammer {
                Image(systemName: "hammer.fill")
                    .resizable()
                    .frame(width: 30, height: 30)
                    .position(self.location)    // < here !!
            }
        }
    }.edgesIgnoringSafeArea(.all)
}

【讨论】:

【解决方案2】:

要获取您点击的位置,您可以执行以下操作:

import SwiftUI

struct ContentView: View {

@State var points:[CGPoint] = [CGPoint(x:0,y:0), CGPoint(x:50,y:50)]

var body: some View {

    ZStack{
        GetTapLocation {
           // tappedCallback
           location in
            self.points.append(location)
            print(self.points)
        }
    }
}
}


struct GetTapLocation:UIViewRepresentable {
var tappedCallback: ((CGPoint) -> Void)

func makeUIView(context: UIViewRepresentableContext<GetTapLocation>) -> UIView {
    let v = UIView(frame: .zero)
    let gesture = UITapGestureRecognizer(target: context.coordinator,
                                         action: #selector(Coordinator.tapped))
    v.addGestureRecognizer(gesture)
    return v
}

class Coordinator: NSObject {
    var tappedCallback: ((CGPoint) -> Void)
    init(tappedCallback: @escaping ((CGPoint) -> Void)) {
        self.tappedCallback = tappedCallback
    }
    @objc func tapped(gesture:UITapGestureRecognizer) {
        let point = gesture.location(in: gesture.view)
        self.tappedCallback(point)
    }
}

func makeCoordinator() -> GetTapLocation.Coordinator {
    return Coordinator(tappedCallback:self.tappedCallback)
}

func updateUIView(_ uiView: UIView,
                   context: UIViewRepresentableContext<GetTapLocation>) {
}

}

必须有一个更简单的实现,但在此之前你可以得到你点击的位置。我希望这会有所帮助:)

【讨论】:

  • 谢谢,但图像保持稳定,我希望它显示在用户点击的位置。
  • 然后你可以使用我的最后一个例子,当你点击图像时,图像会在同一个地方保持稳定,将它替换为你定义的那个。或者你是什么意思用户点击的地方?
  • 但我不想敲击锤子。起初锤子是不可见的,当在屏幕上点击时,锤子应该出现在被点击的地方。
  • 我更新了我的答案,使用第一个代码,您可以单击屏幕上的任意位置,点击的位置将存储在变量点中。希望对你有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 2020-04-17
  • 2019-02-20
  • 2015-11-03
  • 2017-01-03
  • 2021-03-02
相关资源
最近更新 更多