Ios Swift tvOS-当用户按下菜单按钮时暂停图像动画

Ios Swift tvOS-当用户按下菜单按钮时暂停图像动画,ios,swift,animation,uiimage,ios-animations,Ios,Swift,Animation,Uiimage,Ios Animations,对于我的介绍页面,我有多个与音乐同步的图像和动画来制作一个很酷的介绍页面。问题是当我点击菜单并返回应用程序时,所有动画都已完成。我已经实现了暂停音乐的功能,但需要对UIImage动画执行相同的操作。仅供参考,这是一首与歌曲中的人物同步的《凡人快拍》主题曲。我以前没有使用过动画,所以在其他的解决方案上看了看,并尝试了一下,但都没有用 // Mortal Kombat Good Guys @IBOutlet weak var luiKangImage: UIImageView! @IBOutlet

对于我的介绍页面,我有多个与音乐同步的图像和动画来制作一个很酷的介绍页面。问题是当我点击菜单并返回应用程序时,所有动画都已完成。我已经实现了暂停音乐的功能,但需要对UIImage动画执行相同的操作。仅供参考,这是一首与歌曲中的人物同步的《凡人快拍》主题曲。我以前没有使用过动画,所以在其他的解决方案上看了看,并尝试了一下,但都没有用

// Mortal Kombat Good Guys
@IBOutlet weak var luiKangImage: UIImageView!
@IBOutlet weak var johnnyCageImage: UIImageView!
@IBOutlet weak var raidenImage: UIImageView!
@IBOutlet weak var kitanaImage: UIImageView!
@IBOutlet weak var sonyaImage: UIImageView!
@IBOutlet weak var nightWolfImage: UIImageView!
@IBOutlet weak var cassieCageImage: UIImageView!
@IBOutlet weak var kenshiTakahashiImage: UIImageView!
@IBOutlet weak var kungLaoImage: UIImageView!
@IBOutlet weak var jaxBriggsImage: UIImageView!

// Mortal Kombat Bad Guys
@IBOutlet weak var scorpionImage: UIImageView!
@IBOutlet weak var kanoImage: UIImageView!
@IBOutlet weak var reptileImage: UIImageView!
@IBOutlet weak var subZeroImage: UIImageView!
@IBOutlet weak var shangTsungImage: UIImageView!
@IBOutlet weak var goroImage: UIImageView!
@IBOutlet weak var shaoKhanImage: UIImageView!
@IBOutlet weak var tanyaImage: UIImageView!
@IBOutlet weak var quanChiImage: UIImageView!
@IBOutlet weak var mileenaImage: UIImageView!

var player: AVAudioPlayer?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    //Set to execute function playSound
    playSound()

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    setInMotionImagePaths()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    setInMotionImageTimePaths()
}

func setInMotionImagePaths() {
    // Mortal Kombat Good Guys Image Animation Paths
    luiKangImage.center.x -= view.bounds.width
    johnnyCageImage.center.y += view.bounds.width
    raidenImage.center.y -= view.bounds.width
    kitanaImage.center.x -= view.bounds.width
    sonyaImage.center.x -= view.bounds.width
    nightWolfImage.center.y += view.bounds.width
    cassieCageImage.center.y -= view.bounds.width
    kenshiTakahashiImage.center.y += view.bounds.width
    kungLaoImage.center.x -= view.bounds.width
    jaxBriggsImage.center.y -= view.bounds.width

    // Mortal Kombat Bad Guys Image Animation Paths
    scorpionImage.center.x += view.bounds.width
    kanoImage.center.y += view.bounds.width
    reptileImage.center.x += view.bounds.width
    subZeroImage.center.x += view.bounds.width
    shangTsungImage.center.y += view.bounds.width
    goroImage.center.x += view.bounds.width
    shaoKhanImage.center.y += view.bounds.width
    tanyaImage.center.x += view.bounds.width
    quanChiImage.center.y += view.bounds.width
    mileenaImage.center.x += view.bounds.width
}

func setInMotionImageTimePaths() {
    playSound()

    // Mortal Kombat Good Guys Animation Time and Path
    UIView.animate(withDuration: 5.0, delay: 0.6, options: [], animations: {
        self.luiKangImage.center.x += self.view.bounds.width
    }, completion: nil)
    //DONT TOUCH TIME
    UIView.animate(withDuration: 5.0, delay: 2.3, options: [], animations: {
        self.johnnyCageImage.center.y -= self.view.bounds.width
    }, completion: nil)
    //DONT TOUCH TIME
    UIView.animate(withDuration: 5.0, delay: 1.2, options: [], animations: {
        self.raidenImage.center.y += self.view.bounds.width
    }, completion: nil)
    //DONT TOUCH TIME
    UIView.animate(withDuration: 5.0, delay: 10.0, options: [], animations: {
        self.kitanaImage.center.x += self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 8.5, options: [], animations: {
        self.sonyaImage.center.x += self.view.bounds.width
    }, completion: nil)
    //DONT TOUCH TIME
    UIView.animate(withDuration: 5.0, delay: 11.0, options: [], animations: {
        self.nightWolfImage.center.y -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 12.0, options: [], animations: {
        self.cassieCageImage.center.y += self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 13.0, options: [], animations: {
        self.kenshiTakahashiImage.center.y -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 14.0, options: [], animations: {
        self.kungLaoImage.center.x += self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 15.0, options: [], animations: {
        self.jaxBriggsImage.center.y += self.view.bounds.width
    }, completion: nil)

    // Mortal Kombat Bad Guys Animation Time and Path
    UIView.animate(withDuration: 4.0, delay: 5.5, options: [], animations: {
        self.scorpionImage.center.x -= self.view.bounds.width
    }, completion: nil)
    //DONT TOUCH TIME
    UIView.animate(withDuration: 2.0, delay: 0.0, options: [], animations: {
        self.kanoImage.center.y -= self.view.bounds.width
    }, completion: nil)
    //DONT TOUCH TIME
    UIView.animate(withDuration: 5.0, delay: 11.0, options: [], animations: {
        self.reptileImage.center.x -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 4.0, delay: 7.0, options: [], animations: {
        self.subZeroImage.center.x -= self.view.bounds.width
    }, completion: nil)
    //DONT TOUCH TIME
    UIView.animate(withDuration: 5.0, delay: 10.3, options: [], animations: {
        self.shangTsungImage.center.y -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 11.5, options: [], animations: {
        self.goroImage.center.x -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 12.5, options: [], animations: {
        self.shaoKhanImage.center.y -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 13.5, options: [], animations: {
        self.tanyaImage.center.x -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 15.0, options: [], animations: {
        self.quanChiImage.center.y -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 5.0, delay: 14.5, options: [], animations: {
        self.mileenaImage.center.x -= self.view.bounds.width
    }, completion: nil)
}

func playSound() {
    let url = Bundle.main.url(forResource: "MortalKombatSong", withExtension: "mp3")!
    do {
        player = try AVAudioPlayer(contentsOf: url)
        guard let player = player else { return }
        player.prepareToPlay()
        player.play()
        player.numberOfLoops = 0
    } catch let error {
        print(error.localizedDescription)
    }
}

func stopSound() {
    if player?.isPlaying ?? true {
        player?.stop()
    }
}
我如何在应用程序代理中暂停音乐

import AVFoundation

var player: AVAudioPlayer?

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    player?.pause()
}
你可以试试。我认为不可能暂停实际视图动画,因为它们在传递到动画块时就开始了,并且您不会得到任何对它们的引用