LoginSignup
1
2

More than 3 years have passed since last update.

UIViewのExtensionでフェードイン・アウトアニメーションを実装する

Posted at

UIViewでフェードイン・アウトをExtensionとして実装しました。

環境

  • Swift 5
  • Xcode 11.2.1

フェードイン・アウトを行うExtension

UIViewを継承しているクラスなら利用できるので案外便利


extension UIView {

  // MARK: Animation 

  enum Fade {
    case `in`
    case out
  }

  /// フェードインアウトを行う
  /// - Parameters:
  ///   - type: fadeタイプ
  ///   - animation: アニメーションを実行するか
  func fade(type: Fade, animation: Bool = true) {
    switch type {
    case .in:
      isHidden = false
      UIView.animate(withDuration: 0.3) {
        self.alpha = 1.0
      }
    case .out:
      if animation {
        isHidden = true
        UIView.animate(withDuration: 0.3) {
          self.alpha = 0.0
        }
      } else {
        alpha = 0.0
        isHidden = true
      }
    }
  }

}

あとがき

簡潔に分岐処理を書けてない気がするのでもっとこうしたらスマートになるよ!とかあったら教えてください

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2