2017-12-13 3 views
2

スライドを適用しようとしています。私はスレッド1:致命的なエラー:画像にエフェクトを適用しているときにオプションの値をアンラッピングしている間に予期せずnilが見つかりました

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 

を取得し、ボタンのいずれかに任意のヘルプをクリックするたびに続い が

@IBAction func fadeIn(_ sender: Any) { 
     imageView.alpha=0 
     UIView.animate(withDuration: 1, animations: { 
      self.imageView.alpha=1 
     }) 
    } 

    @IBAction func slideIn(_ sender: Any) { 
     imageView.center=CGPoint(x:imageView.center.x-500,y:imageView.center.y) 
     UIView.animate(withDuration: 2) { 
      self.imageView.center=CGPoint(x:self.imageView.center.x+500,y:self.imageView.center.y) 
     } 

    } 

    @IBAction func grow(_ sender: Any) { 
     imageView.frame=CGRect(x:0,y:0,width:0,height:0) 
     UIView.animate(withDuration: 1, animations: { 
      self.imageView.frame=CGRect(x:0,y:0,width:200,height:200) 
     }) 
    } 

私のコードであることをいただければ幸いです。

+0

可能な重複[ "致命的なエラー:予期せずオプションの値アンラップしながら、nilを見つけ、" 何?意味を](https://stackoverflow.com/questions/32170456/what -does-fatal-error-unexpectedly-found-nil-while-unwrap-an-optional-valu) – the4kman

+0

あなたの 'imageView'宣言コードを表示します。それはIBOutletですか? – Krunal

答えて

1

あなたのimageViewべき/かもしれIBOutlet変数/プロパティ(あなたが問題になっている情報によると、あなたの質問に不完全な情報を提供しているので、問題の正確なルートを言うことができない。私はこれを提案することができます)ストーリーボード/ XIBビューコントローラのインターフェイス要素に接続していない可能性があります。

Solution:
Connect (or reconnect) your imageView with your interface element.

enter image description here

0
Hope all functions are correct for animation. You should first get unwrapped value then try to do further operation. It will result to avoid crash 

IBAction func fadeIn(_ sender: Any) { 
     if let img = imageView { 
      img.alpha=0 
      UIView.animate(withDuration: 1, animations: { 
       self.img.alpha=1 
      }) 
     } 
    } 

    @IBAction func slideIn(_ sender: Any) { 
     if let img = imageView { 
      img.center=CGPoint(x:img.center.x-500,y:img.center.y) 
      UIView.animate(withDuration: 2) { 
       self.img.center=CGPoint(x:self.img.center.x+500,y:self.img.center.y) 
      } 
     } 

    } 

    @IBAction func grow(_ sender: Any) { 
     if let img = imageView { 
      img.frame=CGRect(x:0,y:0,width:0,height:0) 
      UIView.animate(withDuration: 1, animations: { 
       self.img.frame=CGRect(x:0,y:0,width:200,height:200) 
      }) 
     } 
    } 
関連する問題

 関連する問題