【Xcode/Swift】画像をアニメーション表示してみる(UIViewPropertyAnimator)

Contents 非表示

完成形


Storyboard

ButtonTagを付与しておいてください。


コード

import UIKit

class ViewController: UIViewController {

    private var animator = UIViewPropertyAnimator()

    @IBOutlet private weak var bigWave: UIImageView!
    @IBOutlet private weak var neoBigWave: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func showAnimation(_ sender: UIButton) {
        switch sender.tag {
        case 1:
            animator = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
                self.bigWave.center.y -= 500
                self.neoBigWave.center.y -= 500
            }
        case 2:
            animator = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
                self.bigWave.center.y += 500
                self.neoBigWave.center.y += 500
            }
        case 3:
            animator = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
                self.bigWave.center.x -= 500
                self.neoBigWave.center.x -= 500
            }
        case 4:
            animator = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
                self.bigWave.center.x += 500
                self.neoBigWave.center.x += 500
            }
        default:
            break
        }
        animator.startAnimation()
    }

}