Ios 点击或拖动时缩放节点功能不一致

Ios 点击或拖动时缩放节点功能不一致,ios,sprite-kit,Ios,Sprite Kit,我希望我的SKSpritenodes在屏幕上点击和拖动时能够放大。当用户删除触摸输入时,他们需要缩小最后一个位置。这是我现在用来实现这一效果的代码: override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches { let location = touch.location(in: self) let card =

我希望我的SKSpritenodes在屏幕上点击和拖动时能够放大。当用户删除触摸输入时,他们需要缩小最后一个位置。这是我现在用来实现这一效果的代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let card = atPoint(location) as? Card
        if card?.cardType == .main{
            card?.zPosition = 30
            card?.run(SKAction.scale(to: 1.3, duration: 0.15))
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let card = atPoint(location) as? Card
        if card?.cardType == .main{
            card?.zPosition = 30
            card?.removeFromParent()
            addChild(card!)
            card?.run(SKAction.scale(to: 1.0, duration: 0.15))
        }
    }
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
将卡=点(位置)设为?卡
如果卡?.cardType=.main{
卡?.zPosition=30
卡片?运行(SKAction.刻度(至:1.3,持续时间:0.15))
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
将卡=点(位置)设为?卡
如果卡?.cardType=.main{
卡?.zPosition=30
卡?.removeFromParent()的
addChild(卡片!)
卡片?运行(SKAction.刻度(至:1.0,持续时间:0.15))
}
}
}
Card是我定义的一个类,它继承自SKSpriteNode


问题似乎是,有时当我触摸精灵节点时,它们不会放大,其他时候会放大。当我拖动它们时,动画看起来非常滞后。此外,当我放下节点(即移除触摸输入)时,缩放效果也不会持续出现(有时会,有时不会)。发生了什么事?如果你们需要更多的源代码,请告诉我。

请不要复制和粘贴此答案,请尝试自己完成工作,并将代码与此答案进行比较

如何解决缩放问题:

调用运行操作时,新操作不会取消上一个操作,因此发生的情况是您同时运行两个操作,这可能会发生冲突。相反,请使用键删除上一个操作

如何解决拖动问题:

您需要保留在touch方法中拖动的节点。然后,当您进行拖动时,您将知道要设置的位置

还要看看我是如何清理代码的,以减少行数,并在许多变量调用中删除“?”的。在你去使用之前?或对于解决问题的变量,检查是否有更好的方法来处理代码流。使用!只有当您能够100%保证nil永远不会发生,并且封装在temp变量中效率很低时。使用?仅当您希望代码继续运行时,即使变量为nil,但将其封装在临时变量中效率低下

如果你需要一个变量和你正在使用的函数在同一个范围内,那么你可能需要考虑使用<代码>守护程序>代码>。守卫的工作方式是,如果不满足某些条件,它将退出当前范围,因此,例如,您在代码中遇到一个点,您说卡必须存在于该点之外,那么您是否执行

guard let card=atPoint(location)as?卡片
。有关guard的更多信息,请参阅以更好地了解其工作原理

var movableCard : Card?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)

        if let card = atPoint(location) as? Card, card.cardType == .main{
            card.zPosition = 30
            movableCard = card
            //Not sure what node this card is suppose to move to
            card.run(SKAction.scale(to: 1.3, duration: 0.15),withKey:"scaling")
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        if let card = movableCard{

            card.position = location
        }
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)

        if let card = movableCard{
            card.zPosition = 30
            card.moveToParent(self)
            movableCard = nil
            card.run(SKAction.scale(to: 1.0, duration: 0.15),withKey:"scaling")
        }
    }
}
var移动卡:卡?
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
如果让card=atPoint(location)作为?card,则card.cardType=.main{
card.zPosition=30
可移动卡
//不确定此卡将移动到哪个节点
卡片运行(SKAction.scale(至:1.3,持续时间:0.15),按键:“缩放”)
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
如果let card=可移动卡{
card.position=位置
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
如果let card=可移动卡{
card.zPosition=30
card.moveToParent(自)
可移动卡=零
卡片运行(SKAction.scale(到:1.0,持续时间:0.15),按键:“缩放”)
}
}
}

对于您的缩放问题,您正在累积操作。删除上一个操作或分配一个键。对于拖动问题,我看不到任何
touchesMoved
功能,但看看如何处理拖动。您还可以将if与let结合起来,以消除可选的“?”<代码>如果让卡=点(位置)为?Card,Card.cardType==.main,在您的触碰端方法中,不要执行删除和添加操作,只需执行
Card.moveToParent(self)
非常感谢您提供的详细答案!