微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果用户将手指从精灵套件中的按钮上移开,如何停止按钮动作?

如何解决如果用户将手指从精灵套件中的按钮上移开,如何停止按钮动作?

 override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
        let touch = touches.first
        let positionInScene = touch!.location(in: self)
        let touchednode = self.atPoint(positionInScene)
        if let name = touchednode.name {
            if name == "leftbutton" {
                print("left button stopped")
                touchednode.run(buttonStoppedPressAction)
                player?.removeAllActions()
            }
            
            if name == "rightbutton" {
                print("right button stopped")
                
                touchednode.run(buttonStoppedPressAction)
                
                player?.removeAllActions()
            }
}
}

这里我有代码,当用户从按钮上松开手指时,它会停止操作,但前提是他们在按钮内抬起手指。因此,如果他们按下它并开始在屏幕上的其他位置移动手指,同时持续按下按钮将不会停止执行其代码。感谢您的帮助。

解决方法

基本上,您应该检查触地时的触摸位置,并与触地时的位置进行比较。如果触摸不再位于您的按钮区域,您将取消所有效果。

首先,有一点。看起来您是在 SKScene 级别处理按钮逻辑,这是教程经常告诉您的操作。然而,这可能不是最好的方法。这里的风险,除了 SKScene 的混乱之外,还来自处理多个对象及其对触摸事件的反应,以及多点触控(如果允许)带来的额外复杂性。

几年前,当我开始使用 SpriteKit 时,我觉得这是一个巨大的痛苦。所以我创建了一个按钮类来独立处理所有的触摸逻辑(并在需要发生某些事情时将信号发送回父级)。优点:没有不必要的杂乱,区分对象没有问题,能够确定每个节点的多点触控余量。

我在课堂上为了查看触摸是否在触摸之前没有离开按钮所做的是存储按钮区域的大小(作为对象的参数)和其中的触摸位置。简单简单。

事实上,Apple 不仅默认提供了一个基本的 SKButton 类,这让我一直困惑不已。无论如何,我想你可能要考虑一下。至少对我来说,它每天节省了很多时间。我已经发布了多个具有相同自定义按钮类的成功应用。

编辑:下面是我的准系统 Button 类。

import SpriteKit

class Button: SKNode {
    
    private var background: SKSpriteNode?
    private var icon: SKNode?
    
    private var tapAction: () -> Void = {}
    
    override init() {
        super.init()
        
        isUserInteractionEnabled = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        isUserInteractionEnabled = true
    }
    
    // MARK: Switches
    
    public func switchButtonBackground(buttonBackgroundSize: CGSize,buttonBackgroundColor: SKColor) {
        background = SKSpriteNode(color: buttonBackgroundColor,size: buttonBackgroundSize)
        addChild(background!)
    }
    
    public func switchButtonIcon(_ buttonIcon: SKNode) {
        if icon != nil {
            icon = nil
        }
        
        icon = buttonIcon
        addChild(icon!)
    }
    
    public func switchButtonTapAction(_ buttonTapAction: @escaping () -> Void) {
        tapAction = buttonTapAction
    }
    
    // MARK: Touch events
    
    override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
        tapAction()
    }
}

然后您通过首先启动它来创建 Button 对象,使用大小和颜色为其分配背景,然后为其分配一个图标,为其分配一个点击时运行的功能,最后将其作为子项添加到场景中。

let icon = SKNode()
let size = CGSize(width: 20.0,height: 20.0)

let button = Button()
button.switchButtonBackground(buttonBackgroundSize: size,buttonBackgroundColor: .clear)
button.switchButtonIcon(icon)
button.switchButtonTapAction(buttonPressed)
addChild(button)

背景定义了按钮的触摸区域,您可以为其指定颜色或将其确定为 .clear。该图标应该在按钮顶部放置您想要的任何文本或图像。只需将它们打包到 SKNode 中即可。如果你想运行一个带有参数作为点击动作的函数,你可以制作一个代码块。

希望有帮助!如果您需要任何进一步的帮助,请告诉我:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。