ios - Swift:为什么点击手势识别器不是每次都能工作?

标签 ios swift xcode uigesturerecognizer uitapgesturerecognizer

我有一个奇怪的问题。我在 subview 中添加了一个基本的点击手势识别器,但它仅在需要时才起作用。有时,它会在第二次单击时起作用,有时在第三次甚至更多次单击时起作用。但是,当我将 print("whatever") 添加到由手势识别器调用的函数时,它总是在我第一次单击 subview 时起作用。

如何解决这个问题?

这两个例子出现了同样的问题:

subview :

let getGiftGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.getGift))
self.continueButton.addGestureRecognizer(getGiftGestureRecognizer)
        DispatchQueue.main.async {
            self.coverAnyUIElement(element: self.successView)
            let gift = SCNNode()
             gift.geometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)

             //nahodne vygenerovat geometry, nebo barvu
             let ownedGeometries = UserDefaults.standard.array(forKey: "ownedGeometries")
             let ownedColors = UserDefaults.standard.array(forKey: "ownedColors")
             let notOwnedGeometriesCount = geometriesCount - ownedGeometries!.count
             let notOwnedColorsCount = colorsCount - ownedColors!.count

             if notOwnedGeometriesCount == 0{
                 //mam vsechny geometrie
                 //vygeneruju barvu

             }else if notOwnedColorsCount == 0{
                 //mam vsechny barvy
                 //vygeneruju geometrii
             }else if(notOwnedGeometriesCount == 0 && notOwnedColorsCount == 0){
                 //mam uplne vsechno

             }else{
                 //vygeneruj cokoliv
                 let random = Int.random(in: 0...10)
                 if random <= 3{

                     //vygeneruj geometrii
                 }else{
                     //vygeneruj barvu
                 }
             }
             print(self.generateColor())
             print(self.generateGeometry())



            self.chest.opacity = 1
             self.chest.position.y = -1.8
             self.world.addChildNode(self.chest)
             let viko = self.chest.childNode(withName: "viko", recursively: false)
             self.chest.addChildNode(gift)

            print("VLAKNO1: \(Thread.isMainThread)")
             viko?.runAction(SCNAction.rotateBy(x: 0, y: 0, z: -90/(180/CGFloat.pi), duration: 1),completionHandler: {
                print("VLAKNO2: \(Thread.isMainThread)")

                    let pohyb = SCNAction.move(by: SCNVector3(0,4,0), duration: 1)
                    gift.runAction(pohyb,completionHandler: {
                       print("VLAKNO3: \(Thread.isMainThread)")

                            self.showConfirmGiftButton()


                    })


             })
        }
    }

按钮:

confirmGiftButton.addTarget(self, action: #selector(acceptedGift), for: .touchUpInside)
@objc func acceptedGift(){
        DispatchQueue.main.async {
            UIButton.animate(withDuration: 0.2, animations: {
                self.confirmGiftButton.alpha = 0.9
            }) { (_) in
                UIButton.animate(withDuration: 0.2, animations: {
                    self.confirmGiftButton.alpha = 1
                }) { (_) in

                    //print("VLAKNO1: \(Thread.isMainThread)")
                    self.chest.runAction(SCNAction.fadeOut(duration: 0.5),completionHandler: {
                       // print("Vlakno: \(Thread.isMainThread)")
                        self.chest.removeFromParentNode()
                        self.moveToMenu()
                    })
                    //print("VLAKNO2: \(Thread.isMainThread)")
                    self.coverAnyUIElement(element: self.confirmGiftButton)
                }
            }
        }
    }

最佳答案

所以我从这两种方法中删除了 DispatchQueue.main.async 并尝试了几次,看起来这就是问题所在,因为现在 subview 手势识别器和按钮目标都可以工作首先点击正确。

我认为这是因为当我单击按钮时,主线程已经被使用,所以它以某种方式跳过了这些方法。并且在第二次或更多次点击时起作用,因为那时它已经是空的。

关于ios - Swift:为什么点击手势识别器不是每次都能工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59650910/

相关文章:

javascript - Facebook 页面插件/提要链接在 iphone x safari 上不起作用

ios - CIFilter 减慢滚动速度

xcode - NSClasses 与 Swift 新类

ios - swift 3 : compress ImageView Vertically depending on its image height

ios - Swift iOS - 在 gameScene 中设置高分

swift - 我的 xcworkspace 有问题吗?

ios - 如何在 iOS 中获取任意线程的正确帧指针?

ios - Core Text 有时无法生成结果 [iOS]

ios - 在 web 服务代码中解包 Optional 值时意外发现 nil

ios - 在我的应用程序中添加设置页面(Xcode 和 Swift)