点击手势识别器 Xcode
Posted
技术标签:
【中文标题】点击手势识别器 Xcode【英文标题】:tap gesture recognizer Xcode 【发布时间】:2021-09-27 12:39:53 【问题描述】:我想在 Xcode,SceneKit 上制作一个 3d 游戏。为了测试它,我开始了一个关于一个盒子的简单项目,如果你点击它,它会在 X 方向移动。我的问题是如何让轻击手势在 3d 游戏中发挥作用?我一直在尝试添加它,但它不起作用。这是我写的所有代码,以防我做错了什么:
import UIKit
import QuartzCore
import SceneKit
class GameViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
let scene = SCNScene(named:"art.scnassets/Box.scn")
let newscene = self.view as! SCNView
newscene.scene = scene
let tap = UITapGestureRecognizer(target: self, action: #selector(taps(tap:)));newscene.addGestureRecognizer(tap)
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
override var shouldAutorotate: Bool
return true
override var prefersStatusBarHidden: Bool
return true
override var supportedInterfaceOrientations: UIInterfaceOrientationMask
if UIDevice.current.userInterfaceIdiom == .phone
return .allButUpsideDown
else
return .all
@objc func taps(tap:UITapGestureRecognizer)
let newscene = self.view as! SCNView
let scene = SCNScene(named: "art.scnassets/Box.scn")
let box = scene?.rootNode.childNode(withName: "Box", recursively: true)
let place = tap.location(in: newscene)
let tapped = newscene.hitTest(place, options: [:])
if tapped.count > 0
box?.runAction(SCNAction.repeatForever(SCNAction.moveBy(x: 5, y: 0, z: 0, duration: 1)))
【问题讨论】:
【参考方案1】:试试这个...
@objc func handleTap(recognizer: UITapGestureRecognizer)
if(data.isNavigationOff == true) return // No panel select if Add, Update, EndWave, or EndGame
if(gameMenuTableView.isHidden == false) return // No panel if game menu is showing
let location: CGPoint = recognizer.location(in: gameScene)
if(data.isAirStrikeModeOn == true)
let projectedPoint = gameScene.projectPoint(SCNVector3(0, 0, 0))
let scenePoint = gameScene.unprojectPoint(SCNVector3(location.x, location.y, CGFloat(projectedPoint.z)))
gameControl.airStrike(position: scenePoint)
else
let hitResults = gameScene.hitTest(location, options: hitTestOptions)
for vHit in hitResults
if(vHit.node.name?.prefix(5) == "Panel")
// May have selected an invalid panel or auto upgrade was on
if(gameControl.selectPanel(vPanel: vHit.node.name!) == false) return
return
如果空袭 - 点击在您触摸的屏幕上投下炸弹,将其转换为 3D
Else hittest 查找可能返回多个结果的面板
【讨论】:
在代码中放置“gameControl”和“data”。这些是什么意思? 抱歉,方便粘贴。 GameControl 是一个自定义的 renderLoop - 启动 attackWave、循环播放游戏片段或其他任何东西。数据只是我跟踪许多变量的一个类。 感谢您回答我。我没有完全使用你的答案,但它让我找到了一种方法。 别担心,很高兴你明白了。以上是关于点击手势识别器 Xcode的主要内容,如果未能解决你的问题,请参考以下文章