トグルでボタンを使用したいのですが、一度クリックすると&イメージが無期限に回転します。もう一度クリックすると画像が停止し、もう一度クリックすると再起動します。無限に回転する画像を停止する適切な方法はありますか?どのようにremoveAllAnimationsを実装していますか?
私は、アニメーションの継続を得ることにこの回答が役に立ったと評価: Rotate a view for 360 degrees indefinitely in Swift?
しかし、私は物事を停止する方法については不明です。 &以下のコードを実装しましたが、これはうまくいくようですが、これがアニメーションを停止する適切な方法であるのか、別の方法があるのか不思議です。また、私のローテーションは終了するまで続きますが、ボタンを押したときの位置で回転をフリーズすることができますか(私は.removeAllAnimations()を2回目の試みで試しましたが、すべて。
@IBOutlet weak var imageView: UIImageView!
var stopRotation = true
func rotateView(targetView: UIView, duration: Double = 1.0) {
if !stopRotation {
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat(Double.pi))
}) { finished in
self.rotateView(targetView: targetView, duration: duration)
}
}
}
@IBAction func spinPressed(_ sender: UIButton) {
stopRotation = !stopRotation
if !stopRotation {
rotateView(targetView: imageView)
}
}
これは、作業を行います。アニメーションの半ばスピンを停止させることが可能になりたければ私も思っていた。それがセットアップされた方法で、アニメーションが停止する前に、完全な180度になります。私はしましたまたspinPressedアクションでremoveAnimationを追加し、rotateView内部stopRotationチェックを退治しようとしたが、それは動作していないよう - 回転が&がちょうどspinPressedが再び押された場合(下記参照)より速くなる続け:
@IBOutlet weak var imageView: UIImageView!
var stopRotation = true
func rotateView(targetView: UIView, duration: Double = 1.0) {
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat(Double.pi))
}) { finished in
self.rotateView(targetView: targetView, duration: duration)
}
}
@IBAction func spinPressed(_ sender: UIButton) {
stopRotation = !stopRotation
if stopRotation {
imageView.layer.removeAllAnimations()
} else {
rotateView(targetView: imageView)
}
}
最初のアプローチが健全かどうかを確認してください。そして、ローテーション中盤を止める方法があれば、それも歓迎です(また、removeAllAnimationsに関する私の間違った考え方に私をまっすぐに設定することもできます)。
ありがとうございます! JG
'finished'ブロックの' stopRotation'フラグの状態をチェックしているのでしょうか?私が使っている実装はCore Animationを直接使用しているので、層に供給されたアニメーションキーを使ってアニメーションをコントロールすることができます – MadProgrammer