iOS) UIView 에 transition animation 적용해보기

less than 1 minute read

UIView transition animation options

먼저 개발자문서를 살펴보자 transition(with:duration:options:animations:completion:)

  • 특정 container 뷰의 transition animation 을 만드는 함수이다.

animation 효과를 주기 위해서 파라미터 options 에 해당하는 UIView.AnimationOptions 옵션을 설정해주면 된다.

준비

  • Main.storyboard

  • ViewController.swift
import UIKit

class ViewController: UIViewController {

    private var isInitialImage = true
    
    @IBOutlet weak var initialImageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initialImageView.image = UIImage(named: "initialImg")
        initialImageView.contentMode = .scaleAspectFill
    }

    @IBAction func touchTransitionButton(_ sender: Any) {
        print("transitionButton touched.")
        
        // ✅ 앞면의 경우
        if isInitialImage {
            self.isInitialImage = false
            initialImageView.image = UIImage(named: "transitionImg")
            
            // ✅ options 파라미터에 원하는 효과에 해당하는 옵션을 넣어주며 된다.
            UIView.transition(with: initialImageView, duration: 1, options:.transitionFlipFromLeft, animations: nil, completion: nil)
        }
        // ✅ 뒷면의 경우
        else {
            self.isInitialImage = true
            initialImageView.image = UIImage(named: "initialImg")
            UIView.transition(with: initialImageView, duration: 1, options: .transitionFlipFromLeft, animations: nil, completion: nil)
        }
    }
}

UIView.AnimationOptions constants.

transition animation 형태에 직접적인 옵션들을 실습해보았다.

Categories:

Updated: