【问题标题】:Swift / SpriteKit Button Drag Off Cancel Not Working?Swift / SpriteKit 按钮拖动取消取消不起作用?
【发布时间】:2016-09-06 09:02:51
【问题描述】:

您好,我需要您的帮助我一直在寻找这个问题的答案。

我还简化了代码,以消除您不需要知道的信息/垃圾。

我有一个使用 SpriteKit 和 Swift 的 IOS 游戏的主菜单场景。有一个主菜单播放按钮。我想要的是当按下按钮时,按钮会变大一点。当我的手指松开时,按钮变小。使用 override func touchesBegan / touchesEnded 可以正常工作。我的问题是当我的手指被拖离按钮时,就像取消一样。按钮不会恢复到原始大小。我很确定我需要使用

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {} 

但是经过多次尝试,当我的手指被拖离按钮时,我没有得到想要的结果,即让按钮恢复到原始大小。感谢您的帮助。

import Foundation
import SpriteKit
import UIKit

class StartScene: SKScene {

    var playButton: SKNode! = nill  

override func didMoveToView(view: SKView) {

        // Create PlayButton image
        playButton = SKSpriteNode(imageNamed: "playButtonStatic")
        // location of Button
        playButton.position = CGPoint(x:CGRectGetMidX(self.frame), y:520);
        self.addChild(playButton)
        playButton.setScale(0.5)
}



    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
                if playButton.containsPoint(location) {
                   playButton.setScale(0.6)}}}



override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)
            if playButton.containsPoint(location) {
                playButton.setScale(0.5)
        }}}



// Problem Area

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    for touch: AnyObject in touches! {

        let location = touch.locationInNode(self)
            if playButton.containsPoint(location) {
               playButton.setScale(0.5)
        }}}

}

感谢您的回复

【问题讨论】:

    标签: swift


    【解决方案1】:

    发现解决方案在 TouchesEnded 中使用 Else 语句

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    
    for touch: AnyObject in touches {
    
        let location = touch.locationInNode(self)
            if playButton.containsPoint(location) {
                playButton.setScale(0.5)
    
        }
            else
            {
                playButton.setScale(0.5)
        }}
    

    【讨论】:

    • FAARRKKKKKKK 我需要这个。谢谢!!!!多年来,我一直在与如何在按钮内执行此操作作斗争......直到现在。
    • 很高兴帮助我也花了一段时间
    【解决方案2】:

    您说您找到了解决方案,但这里有一个更简单的解决方案:使用我创建的专门为 SpriteKit 按钮设计的自定义类。 Link to the GitHub that has the class file. 有了这个类,你完全不用担心它,它从你的触摸函数中删除了不必要的代码。 JKButtonNode 有 3 种状态:正常、突出显示和禁用。如果您已将其从父级中删除,您也不必担心将其设置为 nil。

    要声明变量,请执行此操作。暂时忽略 playButtonAction 错误。

    var playButton = JKButtonNode(background: SKTexture(imageNamed: "playButtonStatic"), action: playButtonAction)
    

    action 是当按钮被按下和松开时调用的函数。在类中的任何位置创建这样的函数。

    func playButtonAction(button: JKButtonNode) {
        //Whatever the button does when pressed goes here
    }
    

    然后你可以在 didMoveToView 中设置它的属性。

    playButton.position = CGPoint(x:CGRectGetMidX(self.frame), y:520)
    
    //These are the 3 images you want to be used when pressed/disabled
    //You can create a bigger image for when pressed as you want.
    playButton.setBackgroundsForState(normal: "playButtonStatic", highlighted: "playButtonHighlighted", disabled: "")
    
    addChild(playButton)
    

    就是这样!如果用户将手指移开,JKButtonNode 类本身已经取消了触摸,它也不会调用该函数,除非用户成功按下按钮并松开手指。您还可以执行其他操作,例如禁用它播放声音等。

    使用 JKButtonNode 的一个好处是您不必再到处都有代码,因为它不需要在触摸函数中编写任何代码。

    【讨论】:

    • 这太好了,谢谢
    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多