0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

UIViewをアニメーションさせるとタッチイベントが認識されない

Last updated at Posted at 2022-11-01

問題

UIViewをアニメーションさせると、UIViewに配置したボタンなどのタッチイベントが認識されない。

  • ポップアップを表示して、一定時間後にアニメーションしながら消す(スナックバー)

解決方法

  • UIView.animateのdelayを指定する場合、animateを実行した時点からタッチイベントが発火しなくなるため、TimerでDelayする
  • アニメーション開始まではイベントの捕捉が可能
  • optionsにUIView.AnimationOptions.allowUserInteractionを指定する
Timer.scheduledTimer(timeInterval:  SnackBarView.Delay, target: self, selector: #selector(timerUpdate), userInfo: nil, repeats: false)

@objc private func timerUpdate() {
    UIView.animate(withDuration: SnackBarView.FadeInOutDurationInSeconds,
                   delay: 0,
                   options: [SnackBarView.Transition, .allowUserInteraction],
                   animations: {
                     self.alpha = 0
                     self.center.y += 100
                   },
                    completion: { _ in self.removeFromSuperview() })
}

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?