参考URLを見ながら学習しています。
その中で以下のエラーが出てきてしまったのですが、なぜでしょうか。
エラー箇所
// メニューの位置を取得する
let menuPos = self.menuView.layer.position
上記のコードで以下のエラーが出ます。
hread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
全体のコード
import
1 2class MenuViewcontrollerViewController: UIViewController { 3 4 @IBOutlet weak var menuView: UIView! 5 6 override func viewDidLoad() { 7 super.viewDidLoad()} 8 9 override func viewWillAppear(_ animated: Bool) { 10 super.viewWillAppear(animated) 11 // メニューの位置を取得する 12 let menuPos = self.menuView.layer.position 13 // 初期位置を画面の外側にするため、メニューの幅の分だけマイナスする 14 self.menuView.layer.position.x = -self.menuView.frame.width 15 // 表示時のアニメーションを作成する 16 UIView.animate( 17 withDuration: 0.5, 18 delay: 0, 19 options: .curveEaseOut, 20 animations: { 21 self.menuView.layer.position.x = menuPos.x 22 }, 23 completion: { bool in 24 }) 25 26 } 27 28 // メニューエリア以外タップ時の処理 29 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 30 super.touchesEnded(touches, with: event) 31 for touch in touches { 32 if touch.view?.tag == 1 { 33 UIView.animate( 34 withDuration: 0.2, 35 delay: 0, 36 options: .curveEaseIn, 37 animations: { 38 self.menuView.layer.position.x = -self.menuView.frame.width 39 }, 40 completion: { bool in 41 self.dismiss(animated: true, completion: nil) 42 } 43 ) 44 } 45 } 46 } 47 48 49 50 51 52}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。