首页 > 解决方案 > PanGesture 后按钮位置未正确更新

问题描述

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

这是运动发生之前场景的第一部分。 这是我使用平移手势后的第二部分场景。

正如您从照片中看到的那样,按钮正在按照我的意愿移动。但是,当我单击 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)
        }
    }
}

标签: iosswift

解决方案


因此,经过一段时间试图找出我做错了什么之后,我能够解决这个问题。更正是有位置(在:相机!)

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)
        }
    }
}

推荐阅读