简体   繁体   中英

Swift tvOS - pause UIImage animations when user presses MENU button

For my intro page I have multiple images animations in sync with music to make a cool intro page. Problem is when I tap MENU and return to the app, all of the animations have completed. I have implemented a function to pause the music, yet need to do the same to the UIImage animations. FYI, it is the Mortal Kombat theme song synced with the characters it says in the song. I have not worked with animations before so looking at other solutions on SO and trying them has not worked.

// 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()
    }
}

How I pause the music in App Delegate

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()
}

You could try out this answer . I don't believe it's possible to pause the actual view animations since they start as soon as they're passed to the animation block and you don't get any references to them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM