我无法在使用3d Touch的设备上使用自定义SKSpriteNode按钮与xCode关卡编辑器一起使用。
我有一个按钮子类,主要基于来自apple的DemoBots示例。
基本代码就是这个
enum ButtonIdentifier: String {
case playButton
case pauseButton
}
/// Button responder delegate
protocol ButtonDelegate: class {
func pressed(button button: ButtonNode)
}
class ButtonNode: SKSpriteNode {
public weak var delegate: ButtonDelegate? {
return scene as? ButtonDelegate
}
var isHighlighted = false {
didSet {
// running skactions to colorise buttons and animate
}
}
var identifier: ButtonIdentifier!
/// Code init (when button is created in code)
/// e.g let playButton = ButtonNode(imageNamed: "ButtonImage", identifier: playButton)
init(imageNamed: String, identifier: ButtonIdentifier) {
self.identifier = identifier
let texture = SKTexture(imageNamed: imageNamed)
super.init(texture: texture, color: SKColor.clearColor(), size: texture.size())
name = identifier.rawValue
setup()
}
/// Level editor init (when button is created in level editor)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Ensure that the node has a supported button identifier as its name.
guard let nodeName = name, identifier = ButtonIdentifier(rawValue: nodeName) else {
fatalError("Unsupported button name found.")
}
self.identifier = identifier
setup()
}
private func setup() {
// zPosition
zPosition = 200
// Enable user interaction on the button node to detect tap and click events.
userInteractionEnabled = true
}
#if os(iOS)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
isHighlighted = true
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
guard let scene = scene else { return }
for touch in touches {
let location = touch.locationInNode(scene)
let node = scene.nodeAtPoint(location)
if node === self || node.inParentHierarchy(self) {
runPressedAction()
} else {
isHighlighted = false
}
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
isHighlighted = false
}
#endif
// MARK: - Pressed Action
private func runPressedAction() {
// SKAction that runs a press animation
delegate?.pressed(button: self)
}
}
如果我通过xCode级别编辑器创建按钮,一切都在模拟器或我的iPhone 6上正常工作,但是当使用测试飞行时,我的6s Plus的朋友在按钮上没有触摸输入,他无法按下它们。
如果我在代码中全部创建按钮,即使在3d触控设备上也能正常工作。
为什么它不能使用3D触控设备上关卡编辑器的按钮?我一直试图看看苹果演示机器人的样本,看看我是否错过了一些设置或什么,但我无法弄清楚。 (演示机器人按钮适用于我的朋友iPhone 6s plus)
我知道在3d触摸设备上,即使x / y坐标没有变化,也会调用touchesMoved方法。但是,我不认为这会导致我的问题,因为在代码中创建按钮时,一切都按预期工作。
Xcode级别编辑器中是否有一些设置我缺少允许触摸3d触摸设备?
答案 0 :(得分:0)
经过几天的挫折后,原来这是一个iOS 9的bug。我的朋友在iOS 9.2.x上,在更新到最新的iOS 9.3.x版本后,一切正常,根本不需要更改代码。