ios - 震动不会停止

标签 ios swift sprite-kit

当我“停止 - 完成”游戏场景时,如果发生振动,它不会停止。我不明白为什么。

当玩家碰撞左侧或右侧障碍物时,手机会产生振动。但是,如果我在振动时关闭游戏,它将继续振动,直到 Controller 取消初始化。 为什么?

我什至尝试重新定位播放器,但它仍然振动。

关闭函数

View Controller

func closeGame() {
    if isUserNeedAuthenticating {
        Vibrations.longVibrate()
        viewModel.uploadPoints()
        scene?.stopGame()
        showNextController()
}

游戏场景

func stopGame() {
    gameTimer?.invalidate()
    stopObjectTimers()
    stopMotionManager()
    self.removeAllActions()
    self.removeAllChildren()
}

振动功能

func playerDidCollideWithLeftBarrier() {
    Vibrations.longVibrate()
    let spark = SKEmitterNode(fileNamed: "Sparkling")!
    spark.setScale(0.3)
    player.addChild(spark)
    spark.position = CGPoint(x: -(player.size.width/4), y: 0)
    self.run(SKAction.wait(forDuration: 0.2)) {
        spark.removeFromParent()
    }
}

func playerDidCollideWithRightBarrier() {
    Vibrations.longVibrate()
    let spark = SKEmitterNode(fileNamed: "Sparkling")!
    spark.setScale(0.3)
    player.addChild(spark)
    spark.position = CGPoint(x: (player.size.width/4), y: 0)
    self.run(SKAction.wait(forDuration: 0.2)) {
        spark.removeFromParent()
    }
}

移动功能

override func didSimulatePhysics() {
        let mirrorWidth: CGFloat = 15
        let min = (player.size.width / 2) + mirrorWidth
        let max = frame.width - (player.size.width / 2) - mirrorWidth
        let newPosition = player.position.x + xAcceleration * 30
        if (newPosition < min) {
            playerDidCollideWithLeftBarrier()
            player.position.x = min
            return
        } else if (newPosition > max) {
            player.position.x = max
            playerDidCollideWithRightBarrier()
            return
        } else {
            player.position.x = newPosition
        }
    }


/// A class used to play vibrations
final class Vibrations {

    /// Play a short single vibration, like a tac
    static func tacVibrate() {
        AudioServicesPlaySystemSound(1519) // one tack
    }

    /// Play three shorts tac vibration, like a tac tac tac
    static func threeTacVibrate() {
        AudioServicesPlaySystemSound(1521)
    }

    /// Play a strong boom vibration
    static func boomVibrate() {
        AudioServicesPlaySystemSound(1520)
    }

    /// Play a long vibrations trr trr, it sounds like an error
    static func longVibrate() {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) // heavy tack
    }
}

最佳答案

在您的应用程序委托(delegate)中,在将辞职通知期间,添加以下行:

AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate)

您还应该为需要取消的任何其他 ID 添加它。

您还可以将其添加到您的类(class)中以使其更轻松。

final class Vibrations {

    /// Play a short single vibration, like a tac
    static func tacVibrate() {
        AudioServicesPlaySystemSound(1519) // one tack
    }

    /// Play three shorts tac vibration, like a tac tac tac
    static func threeTacVibrate() {
        AudioServicesPlaySystemSound(1521)
    }

    /// Play a strong boom vibration
    static func boomVibrate() {
        AudioServicesPlaySystemSound(1520)
    }

    /// Play a long vibrations trr trr, it sounds like an error
    static func longVibrate() {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) // heavy tack
    }

    /// Stops the short single vibration, like a tac
    static func stopTacVibrate() {
        AudioServicesDisposeSystemSoundID(1519) // one tack
    }

    /// Stops the three shorts tac vibration, like a tac tac tac
    static func stopThreeTacVibrate() {
        AudioServicesDisposeSystemSoundID(1521)
    }

    /// Stops the strong boom vibration
    static func stopBoomVibrate() {
        AudioServicesDisposeSystemSoundID(1520)
    }

    /// Stops the long vibrations 
    static func stopLongVibrate() {
        AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate) // heavy tack
    }

}

关于ios - 震动不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50371986/

相关文章:

ios - 如何以编程方式创建按钮?

带有用于切换的容器 View 的 iOS Swift 分段控件 - View 不是第一次加载

ios - 为什么我使用框架得到不可用的声明

ios - 在多个 Controller 上执行 segue

ios - 如何在 SpriteKit 中制作响应式 Controller ?

ios - 关联的枚举类型实现 CaseItratable

ios - 可能的 Facebook 文档错误导致 swift 代码无法编译?

ios - 如何在 Swift 中使用 Calendar nextDate 方法查找下一个小时的时间

ios - 为什么 SpriteKit 不显示任何内容?

swift - 如何在 Swift 中使用 deltaTime 正确计算 1 秒