ios - 繰り返し - UIView.animateWithDuration Swift ループアニメーション
usingspringwithdamping (2)
完了ブロックアプローチを行う必要はありません。アニメーションオプション引数を使用してください:
Swift 3.0用に更新されました
UIView.animate(withDuration: 2.0, delay: 0, options: [.repeat, .autoreverse], animations: {
coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)
}, completion: nil)
何らかの理由で後でアニメーションを停止したい場合は、次のようにしてください:
coloredSquare.layer.removeAllAnimations()
ViewController.Swiftでは、ある点から別の点にボックスをアニメーション化することができました。 私はこれをループするのは簡単だろうと思ったので、ボックスは1点にアニメートされ、元の位置にアニメーションされてから再びループします。 私はオブジェクトを位置に移動することができましたし、 "完全な"移動では再び元に戻しますが、ループを作成しません。 これはどのように達成できますか?
私はこれがうまくいくかもしれないと思ったが、私は正直に分かっていない:
let boxmoves = [CGRect(x: 120, y: 220, width: 100, height: 100), CGRect(x: 120, y: 120, width: 100, height: 100)]
for boxmove in boxmoves {
coloredSquare.frame = boxmove
}
デバイスの幅に基づいてどのように中心を置くことができますか(数学が関わっていると仮定していますか?)
私のコード:
let coloredSquare = UIView()
coloredSquare.backgroundColor = UIColor.blueColor()
coloredSquare.frame = CGRect(x: 120, y: 120, width: 100, height: 100)
self.view.addSubview(coloredSquare)
// found repeate but this will not animate as I want.
//UIView.animateWithDuration(2.0, delay: 0.2, options: UIViewAnimationOptions.Repeat, animations: {
UIView.animateWithDuration(2.0, animations: {
coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)
}, completion: { finished in
UIView.animateWithDuration(2.0, animations: {
coloredSquare.frame = CGRect(x: 120, y: 120, width: 100, height: 100)
})
})
UIView.animate(withDuration: 3.0,
delay: 0.0,
options: [.curveLinear, .repeat],
animations: { () -> Void in
coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)
}, completion: { (finished: Bool) -> Void in
})