2017-11-02 16 views
0

UIViewアニメーションの問題:アニメーションを完了した後にもう一度アニメーションが表示されます。UIView.animation Issue

enter image description here

let frameHeight: CGFloat = 44.0 

self.frame = CGRect(x: 0, y: -frameHeight, width: UIScreen.main.bounds.width, height: frameHeight) 

UIView.animate(withDuration: 0.5, animations: { 

      self.frame.origin.y += self.frameHeight 

     }, completion: { _ in 

      UIView.animate(withDuration: 0.5, delay: 1, options: [], animations: { 

       self.frame.origin.y -= self.frameHeight 

      }) 

     }) 

答えて

0

アニメーションが始まる前に、アニメーションブロックは外側のブロックコンテキストと同じ実行ループで実行されていないので、ビューのフレームを計算します。

さらに、frameHeightをローカルに定義している間にアニメーションブロックのself.frameHeightプロパティを参照していることに気付きました。

試してみてください。この(事前計算の原点Y):

let frameHeight: CGFloat = 44.0 
self.frame = CGRect(x: 0, y: -frameHeight, width: UIScreen.main.bounds.width, height: frameHeight) 

// Calcuate destinations before 
let finalOriginDestinationY = self.frame.origin.y 
let firstOriginDestinationY = self.frame.origin.y + frameHeight 

UIView.animate(withDuration: 0.5, animations: { 

    // Set absolute value - otherwise the frame is calculated according to position at current time 
    self.frame.origin.y = firstOriginDestinationY 
}, completion: { _ in 
    UIView.animate(withDuration: 0.5, delay: 1, options: [], animations: { 

     // Set absolute value (Precalculated) 
     self.frame.origin.y = finalOriginDestinationY 
    }) 

})