LoginSignup
1
3

More than 5 years have passed since last update.

[Swift]サイドメニューで右側からメニューを表示する

Last updated at Posted at 2019-06-01

サイドメニューで左側からメニューが出てくる場合の実装例は多数あったものの、
右側からメニューを出す方法が、分からず詰まったため解決方法を記載します。

環境

Xcode:10.2
Swift:5.0.1

実際のコード

右側から表示する場合のアニメーション部分のコードを記載します。
前提として、centerControllerの後ろにメニュー用のviewControllerがある状態だと思ってください。

//右側から表示する場合
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
     self.centerController.view.frame.origin.x = -200
}, completion: nil)

「self.centerController.view.frame.origin.x」がviewのx座標であるため、0であればviewは画面の左側にぴったり寄っている状態です。通常はこの状態です。

右側からメニューを表示するために、x軸の座標をずらします。
x軸が-200になれば、centerControllerのviewが左側にずれるため、下のメニュー用のviewが右側から表示されます。

-200の部分をフレームの横幅を利用して、

self.centerController.view.frame.origin.x = -self.centerController.view.frame.width + 80

とすれば画面サイズが異なる端末にも対応できます。

引用

以下の動画と動画に出てくるソースを参考にしております。

https://www.youtube.com/watch?v=dB-vB9uDRCI&t=1129s
https://github.com/sdowless/SideMenu

1
3
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
3