ios - 平移手势后按钮位置未正确更新

标签 ios swift

当我使用平移手势在 Tilemap 背景周围移动时,我已将按钮设置为随相机移动的位置。但是,当我点击按钮时它不起作用。如果我打开场景并且不使用平移手势移动相机,它就会起作用。因此,我假设按钮的位置没有正确更新,尽管它明显随相机移动。

This is the First part of the Scene before movement occurs. This is the second part of the Scene after I have used the Pan Gesture.

正如您从照片中看到的,按钮正在按照我想要的方式移动。但是,当我单击 Rim 图像时,它在第二个图像中无法正常工作。

下面是我的相关代码:

func addButtonstoScene()
{
    //Hint Button
    buttonHint.position = CGPoint(x: (camera?.position.x)! + buttonHintXOffset, y: (camera?.position.y)! + buttonYOffset)
    buttonHint.name = "Hint"
    buttonHint.size = CGSize(width: buttonWidth, height: buttonHeight)
    buttonHint.isHidden = false
    //Fast Forward Button
    buttonFastForward.position = CGPoint(x: (camera?.position.x)! + buttonFastForwardXOffset, y: (camera?.position.y)! + buttonYOffset)
    buttonFastForward.name = "Hint"
    buttonFastForward.size = CGSize(width: buttonWidth, height: buttonHeight)
    buttonFastForward.isHidden = false
    //Settings Button
    buttonSettings.position = CGPoint(x: (camera?.position.x)! + buttonSettingsXOffset, y: (camera?.position.y)! + buttonYOffset)
    buttonSettings.name = "Settings"
    buttonSettings.size = CGSize(width: buttonWidth, height: buttonHeight)
    buttonSettings.zPosition = 2000

    camera?.addChild(buttonHint)
    camera?.addChild(buttonFastForward)
    camera?.addChild(buttonSettings)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    for touch in touches
    {
        let location = touch.location(in: self)
        if buttonSettings.contains(location)
        {
            let transitions: SKTransition = SKTransition.doorway(withDuration: transitionTime)
            let scene: SKScene = OptionsScene(size: CGSize(width: 2048, height: 1536))
            scene.scaleMode = .aspectFill
            self.view?.presentScene(scene, transition: transitions)
        }
    }
}

最佳答案

所以,经过一段时间的尝试找出我做错了什么后,我能够解决这个问题。更正是要有位置(在:相机中!)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    for touch in touches
    {
        let location = touch.location(in: camera!)
        if buttonSettings.contains(location)
        {
            let transitions: SKTransition = SKTransition.doorway(withDuration: transitionTime)
            let scene: SKScene = OptionsScene(size: CGSize(width: 2048, height: 1536))
            scene.scaleMode = .aspectFill
            self.view?.presentScene(scene, transition: transitions)
        }
    }
}

关于ios - 平移手势后按钮位置未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51826309/

相关文章:

ios - 未找到目标,请重新连接设备,Xcode :Device Support File

ios - Swift Firebase 数据库覆盖

ios - Swift 3 NSAttributedString 多个属性

ios - UITableView 不显示图像

ios - 无法以编程方式执行 segue

ios - 委托(delegate)功能在 UIViewController 之外不起作用

ios - 集中放置 Collection View 单元格

ios - 减少文本字段中左 View 和光标之间的间隙

ios - 如何检测用户何时从应用程序中截取屏幕截图?

ios - 为什么我的按钮在 Swift Playgrounds 中不起作用?