Button 尝试使用swift制作按钮时,SKSpriteNode触控开始/结束时未按预期工作
ive子类SKSpriteNode和am试图通过覆盖SKSpriteNode的touchesBegind和touchesEnded方法来实现一个简单的按钮,并添加了两个方法来在按钮按下/释放时设置按钮动画Button 尝试使用swift制作按钮时,SKSpriteNode触控开始/结束时未按预期工作,button,swift,sprite-kit,touchesbegan,Button,Swift,Sprite Kit,Touchesbegan,ive子类SKSpriteNode和am试图通过覆盖SKSpriteNode的touchesBegind和touchesEnded方法来实现一个简单的按钮,并添加了两个方法来在按钮按下/释放时设置按钮动画 import Foundation import SpriteKit class SimpleButton: SKSpriteNode { //called when the user taps the button override func touchesBegan(t
import Foundation
import SpriteKit
class SimpleButton: SKSpriteNode {
//called when the user taps the button
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
println("touches began")
animateForTouchDown()
}
//called when the user finishes touching the button
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
println("touches ended")
animateForTouchUp()
}
override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
println("touches moved")
}
func animateForTouchDown() {
self.runAction(SKAction.scaleBy(0.8, duration: 0.1))
println("animateing for touch down")
}
func animateForTouchUp() {
self.runAction(SKAction.scaleBy(1.25, duration: 0.1))
println("animating for touch up")
}
}
然后将按钮添加到我的场景中,如下所示:
let imageTexture = SKTexture(imageNamed: "a_button")
let sprite: SimpleButton = SimpleButton(texture: imageTexture)
sprite.userInteractionEnabled = true
sprite.position = CGPoint(x:CGRectGetMidX(self.frame) - 200, y:CGRectGetMidY(self.frame));
sprite.color = UIColor.blackColor()
sprite.colorBlendFactor = 1.0
self.addChild(sprite)
这只在按钮的x位置位于屏幕右侧时起作用。如果我将按钮移动到屏幕的左侧(x<200左右),唯一让按钮设置动画的方法是单击并拖动,然后释放。(看起来触摸开始和结束都会在发生这种情况时释放按钮时被调用,这很奇怪)
我不知道为什么会这样。这对我来说毫无意义。我使用相同的代码在我的场景中添加了多个按钮,屏幕右侧的按钮工作得很好,屏幕左侧超过x=200左右的任何按钮都必须单击并拖动才能工作。有什么想法吗?我试过多个模拟器,多个iOS版本,在我运行7.1的设备上测试,它们都有相同的行为
更新:看起来这是因为我在视图控制器中加载了我的场景,该视图控制器使用默认的show(push)动画推送到导航控制器堆栈上。有什么办法可以让我的动画工作吗?如果我在我的序列中选择另一个动画,如“显示细节”或“模式”,它会工作。但我想要的是推送动画,而不是模态 当我实现该按钮时,该按钮在场景中的任何x位置都有效。你更新到最新版本的Xcode了吗?是的,我有最新版本。我想可能是iOS 8。使用8是否对您有效?是的,我将您的代码复制到一个新的iOS 8.0 sprite kit项目中,按钮在不同的x位置对我的点击做出反应(调整大小和NSLog)。确定。这看起来像是发生了,因为我在视图控制器中加载了我的场景,该视图控制器使用segue和动画推送到导航控制器上。可能是动画导致了问题。(我只是在使用默认的推送动画)我只是在一个测试项目中使用了一个没有导航控制器和动画的视图控制器进行了尝试,效果很好。当导航控制器和动画不起作用时。不知道我怎样才能解决这个问题。