2017-11-15 14 views
0

アニメーションが実行される前に私のラベルが消えています。ラベルが非表示になる前にUIViewのアニメーションが終了しない

ボタンを押してからアニメーションを実行し、ラベルとボタンをスライドさせます。しかし、私がボタンを押すと、アニメーションがそれらをスライドさせる前に両方とも隠されています。

func nextGame() { 

    UIView.animate(withDuration: 3, delay: 0.0, options: .allowAnimatedContent, animations: { 
      NSLog("Animation started") 
      self.labelWinningPlayer.center = CGPoint(x: self.labelWinningPlayer.center.x + 500, y: self.labelWinningPlayer.center.y) 
      self.buttonNextGameLabel.center = CGPoint(x: self.buttonNextGameLabel.center.x + 500, y: self.buttonNextGameLabel.center.y) 
     }, completion: { (finished: Bool) in 
      NSLog("Animation stopped") 
      self.labelWinningPlayer.isHidden = true 
      self.buttonNextGameLabel.isHidden = true 
    }) 


    //sets buttons to start position 
    labelWinningPlayer.center = CGPoint(x: labelWinningPlayer.center.x - 1000, y: labelWinningPlayer.center.y) 
    buttonNextGameLabel.center = CGPoint(x: buttonNextGameLabel.center.x - 1000, y: buttonNextGameLabel.center.y) 

    //hides buttons that lay under the animated buttons 
    for i in 1...9 { 

     if let button = view.viewWithTag(i) as? UIButton { 
      button.setImage(nil, for: .normal) 
     } 
    } 
    activeGame = true 
} 

//animation should get started with this button 
@IBAction func buttenNextGameAction(_ sender: Any) { 
    nextGame() 
} 

//slides buttons in 
func slideNextGameButtons() { 

    labelWinningPlayer.isHidden = false 
    buttonNextGameLabel.isHidden = false 

    UIView.animate(withDuration: 0.5, animations: { 

     self.labelWinningPlayer.center = CGPoint(x: self.labelWinningPlayer.center.x + 500, y: self.labelWinningPlayer.center.y) 
     self.buttonNextGameLabel.center = CGPoint(x: self.buttonNextGameLabel.center.x + 500, y: self.buttonNextGameLabel.center.y) 
    }) 
} 

ここにNSLogがあります。それによると、それは完全なアニメーションを実行しています...

2017-11-15 23:26:17.321465 [2442:245232] Animation started 
2017-11-15 23:26:20.325137 [2442:245232] Animation stopped 

おかげさまであなたの助けをありがとう!

+0

にラベルとボタンのスーパービューclipsToBoundsプロパティを設定しますか? – matt

+3

ラベルとボタンは自動レイアウト制約によって配置されていますか? – matt

+0

アニメーションが終了すると、それらは非表示になります。また、自動レイアウトで配置されていないものはありません。 – Ravehorn

答えて

2

あなたのコードは完璧に動作します:

enter image description here

したがって、あなたがはないは問題が何であれの原因となっているについての私達に言った何か。

EDIT:確かに、あなたが問題の原因となったことについて私たちに教えてくれなかったコードでした。

UIView.animate(withDuration: 3, delay: 0.0, options: .allowAnimatedContent, animations: { 
     self.labelWinningPlayer.center = CGPoint(x: self.labelWinningPlayer.center.x + 500, y: self.labelWinningPlayer.center.y) 
     self.buttonNextGameLabel.center = CGPoint(x: self.buttonNextGameLabel.center.x + 500, y: self.buttonNextGameLabel.center.y) 
    }, completion: { (finished: Bool) in 
     self.labelWinningPlayer.isHidden = true 
     self.buttonNextGameLabel.isHidden = true 
}) 


しかし、あなたは、このですライン、を教えていなかった:あなたはこのことについて語ってくれたこれらの行は、アニメーションをキャンセル

labelWinningPlayer.center = CGPoint(x: labelWinningPlayer.center.x - 1000, y: labelWinningPlayer.center.y) 
buttonNextGameLabel.center = CGPoint(x: buttonNextGameLabel.center.x - 1000, y: buttonNextGameLabel.center.y) 


アニメーションの内容を理解していないようです。あなたは最初にアニメートし、の後にのアニメーションの後に、completionの機能を入れます。しかし、これらの行何かしたい何かの後にのアニメーション。だから、それらをcompletionの機能に入れてください!このように:

UIView.animate(withDuration: 3, delay: 0.0, options: .allowAnimatedContent, animations: { 
     self.labelWinningPlayer.center = CGPoint(x: self.labelWinningPlayer.center.x + 500, y: self.labelWinningPlayer.center.y) 
     self.buttonNextGameLabel.center = CGPoint(x: self.buttonNextGameLabel.center.x + 500, y: self.buttonNextGameLabel.center.y) 
    }, completion: { (finished: Bool) in 
     self.labelWinningPlayer.isHidden = true 
     self.buttonNextGameLabel.isHidden = true 
     labelWinningPlayer.center = CGPoint(x: labelWinningPlayer.center.x - 1000, y: labelWinningPlayer.center.y) 
     buttonNextGameLabel.center = CGPoint(x: buttonNextGameLabel.center.x - 1000, y: buttonNextGameLabel.center.y) 
     // ... and everything else in the method goes here too 
}) 


及び方法は他のすべてがあまりにもそこに移動する必要があります。 の後に起こるすべてのものは、の後に、completionの機能に入る。それはcompletionの意味です!

+0

ボタンに関連するすべてのコードに質問のコードを編集しました。多分あなたはそこに何かを見ますか?または、ストーリーボードに問題がある可能性がありますか? – Ravehorn

+0

うん、それはあなたがもともと私たちに見せてくれなかった、追加のコードです、それは問題を引き起こしています。アニメーションが終了した後に起こるように、すべてを 'completion'関数に移動します。 – matt

+0

私の答えを編集して、私の言いたいことを教えてください。次に質問をするときは、コードを隠す代わりに表示してください。あなたはより速く、より良い助けを得るでしょう。 – matt

0

は、ラベルやボタン(アニメーションが終了した)開始時(アニメーションが開始)または末尾に隠され得るか、真

UIView.animate(withDuration: 3, delay: 0.0, options: [], animations: { 
     NSLog("Animation started") 
     self.labelWinningPlayer.center = CGPoint(x: (self.labelWinningPlayer.center.x) * 3, y: self.labelWinningPlayer.center.y) 
     self.buttonNextGameLabel.center = CGPoint(x: (self.buttonNextGameLabel.center.x) * 3, y: self.buttonNextGameLabel.center.y) 
    }, completion: { (finished: Bool) in 
     NSLog("Animation stopped") 
     self.labelWinningPlayer.isHidden = true 
     self.buttonNextGameLabel.isHidden = true 
    }) 
+0

悲しいことに助けてくれませんでしたが、とにかくありがとう – Ravehorn

関連する問題