質問

キャンセル/返金はできる UIView アニメーションです。やばい幅広いステークホルダーのCAはどうすればいいですか?

すなわちって思ったことも設定終了アニメすぎるアクション):

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties

// set view properties

[UIView commitAnimations];

その前にアニメーションが完了し、取得しますアニメーション終了イベントいたい消し(カットでいいます)が運用しています。可能ですか?Googling周りを見ると、少数の人々同様の質問と答えのない、一又は二人の思惑とはできません。

役に立ちましたか?

解決

私はそれを行う方法は、あなたのエンドポイントに新しいアニメーションを作成することです。非常に短い時間を設定して、現在の状態から開始する+setAnimationBeginsFromCurrentState:メソッドを使用してください。あなたがYESに設定すると、現在のアニメーションが短くカットされます。このようになります:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.1];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties

// set view properties

[UIView commitAnimations];

他のヒント

使用します。

#import <QuartzCore/QuartzCore.h>

.......

[myView.layer removeAllAnimations];
特定のビューにのすべてののアニメーション、の直後にのを停止するには、

最も簡単な方法、これです:

QuartzCore.frameworkにプロジェクトをリンクします。あなたのコードの開始時:

#import <QuartzCore/QuartzCore.h>

さて、あなたは彼らのトラックで死んビュー上のすべてのアニメーションを停止する場合は、これを言うます:

[CATransaction begin];
[theView.layer removeAllAnimations];
[CATransaction commit];

真ん中のラインは全て自身で動作しますが、実行ループの終了(「瞬間を再描画」)までの遅延があります。示すように、その遅延を防ぐために、明示的なトランザクションブロック内でコマンドをラップします。その他の変更ありません。この作品は、現在の実行ループの中で、この層の上に行われています。

のiOS 4上と大きく、の短い最初のアニメーションを切断する第二のアニメーションにする UIViewAnimationOptionBeginFromCurrentStateオプションを使用します。

例として、あなたは活動の指標との見解を持っていると仮定します。あなたは、いくつかの潜在的に時間のかかる活動が始まりながら、活動の指標にフェードインしたい、との活動が終了したときにそれをフェードアウト。以下のコードでは、その上に活性インジケーターを有するビューがactivityViewと呼ばれます。

- (void)showActivityIndicator {
    activityView.alpha = 0.0;
    activityView.hidden = NO;
    [UIView animateWithDuration:0.5
                 animations:^(void) {
                     activityView.alpha = 1.0;
                 }];

- (void)hideActivityIndicator {
    [UIView animateWithDuration:0.5
                 delay:0 options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^(void) {
                     activityView.alpha = 0.0;
                 }
                 completion:^(BOOL completed) {
                     if (completed) {
                         activityView.hidden = YES;
                     }
                 }];
}

あなたは、単に現在のUIViewアニメーションの外で、アニメ化されているプロパティを設定する必要がアニメーションをキャンセルします。それはどこにそれはアニメーションを停止し、UIViewのはあなただけで定義された設定にジャンプします。

この答えを復活して申し訳ありませんが、iOS版に10物事はちょっと変わって、今キャンセル可能であり、あなたも優雅にキャンセルすることができます!

iOSの10の後には、 UIViewPropertyAnimatorする

とアニメーションをキャンセルすることができます
UIViewPropertyAnimator(duration: 2, dampingRatio: 0.4, animations: {
    view.backgroundColor = .blue
})
animator.stopAnimation(true)

あなたはtrueを渡す場合は、アニメーションをキャンセルし、あなたがそれをキャンセルどこに右停止します。完成メソッドが呼び出されません。あなたはfalseを渡した場合しかし、あなたはアニメーションの仕上げを担当します:

animator.finishAnimation(.start)

あなたのアニメーションを終了し、現在の状態(.current)にとどまるか、初期状態(.start)または終了状態(.END)

に行くことができます

ところで、あなたも一時停止し、後で再起動することができます...

animator.pauseAnimation()
animator.startAnimation()

注:あなたは突然あなたがあなたのアニメーションを逆たりも、あなたがそれを一時停止した後、あなたのアニメーションを変更することができますキャンセルしたくない場合は、

の場合を使用してアニメーション化制約の変更により一定ではなく、賃料、その他の方法で作iOS8です。

例のアニメ:

self.constraint.constant = 0;
[self.view updateConstraintsIfNeeded];
[self.view layoutIfNeeded];
[UIView animateWithDuration:1.0f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     self.constraint.constant = 1.0f;
                     [self.view layoutIfNeeded];
                 } completion:^(BOOL finished) {

                 }];

【解決

を削除する必要があるのアニメからの層の他の意見に影響されることに制約の変更やそのsublayers.

[self.constraintView.layer removeAllAnimations];
for (CALayer *l in self.constraintView.layer.sublayers)
{
    [l removeAllAnimations];
}
あなただけの一時停止したい場合は、

/

スムーズなアニメーションを停止
self.yourView.layer.speed = 0;

ソース:レイヤツリーでのアニメーションを一時停止する方法

に解決で私にとって、このた:の UIView アニメをセットに直し版です。しかし、これらのようなアニメーション時のプレゼンテーション層のモデルを設定している。

いる私たしていきたい"アニメーションからおうみ"(だいことを意味することができました。したい場合は、その後:

  1. 追加QuartzCore.
  2. CALayer * pLayer = theView.layer.presentationLayer;

設定の位置にプレゼンテーション層

を使用してい数オプションを含む UIViewAnimationOptionOverrideInheritedDuration

でもAppleのドキュメントは漠然としたわかんない場合でもオーバーライドその他のアニメを用いた場合、リセットタイマー.

[UIView animateWithDuration:blah... 
                    options: UIViewAnimationOptionBeginFromCurrentState ... 
                    animations: ^ {
                                   theView.center = CGPointMake( pLayer.position.x + YOUR_ANIMATION_OFFSET, pLayer.position.y + ANOTHER_ANIMATION_OFFSET);
                   //this only works for translating, but you get the idea if you wanna flip and scale it. 
                   } completion: ^(BOOL complete) {}];

ことになるすべきも解決。

[UIView setAnimationsEnabled:NO];
// your code here
[UIView setAnimationsEnabled:YES];

私は同じ問題を抱えています。 APIは、いくつかの特定のアニメーションを取り消すには何もありません。

+ (void)setAnimationsEnabled:(BOOL)enabled

すべてのアニメーションを無効にし、ひいては私のために動作しません。 2つのソリューションがあります:

1)あなたのアニメーションオブジェクトサブビューします。あなたはそのビューのアニメーションをキャンセルしたいときに、ビューを削除するか、それを隠します。非常にシンプルですが、あなたはビューでそれを維持する必要がある場合は、アニメーションせずにサブビューを再作成する必要があります。

2)このように、アニメーションだけを繰り返し、必要に応じてアニメーションを再起動するデリゲートセレクタを作ります

-(void) startAnimation {
NSLog(@"startAnim alpha:%f", self.alpha);
[self setAlpha:1.0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(pulseAnimationDidStop:finished:context:)];
[self setAlpha:0.1];
[UIView commitAnimations];
}

- (void)pulseAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if(hasFocus) {
    [self startAnimation];
} else {
    self.alpha = 1.0;
}
}

-(void) setHasFocus:(BOOL)_hasFocus {
hasFocus = _hasFocus;
if(hasFocus) {
    [self startAnimation];
}
}

2通報は)常にそれが現在のアニメーションサイクルを終了してアニメーションを停止することがあり遅らせるということです。

この情報がお役に立てば幸いです。

あなたがキャンセルした場合でも、アニメーションdidStopSelector上記の方法でアニメーションがまだ実行されます。あなたがアニメーションで駆動されるアプリケーション内の論理状態を持っているのであれば、あなたは問題が発生します。 I、上記の方法でこのような理由からUIViewアニメーションのコンテキスト変数を使用します。あなたがアニメーションにコンテキストのparamによって、プログラムの現在の状態を渡すとアニメーションが停止したときに、それが何かをするか、単に現在の状態とコンテキストとして渡された状態値に基づいて返す必要がある場合は、あなたのdidStopSelector機能が決めることができる。

スティーヴン・ダーリントンのソリューションのスウィフトバージョン

UIView.beginAnimations(nil, context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(0.1)
// other animation properties

// set view properties
UIView.commitAnimations()
CALayer * pLayer = self.layer.presentationLayer;
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.001 animations:^{
    self.frame = pLayer.frame;
}];

元のまたは最終の状態を元に戻すことなく、アニメーションを一時停止する

CFTimeInterval paused_time = [myView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
myView.layer.speed = 0.0;
myView.layer.timeOffset = paused_time;

私はUIStackView以外に、私はremoveAllAnimations()は、予測不可能な状態にそれらを設定することができますので、1を初期ためにいくつかの値を設定する必要があり、removeAllAnimations()アニメーションで作業する場合。私は内部のstackViewview1view2しており、1つのビューが表示されている必要があり、1つは隠されます:

public func configureStackView(hideView1: Bool, hideView2: Bool) {
    let oldHideView1 = view1.isHidden
    let oldHideView2 = view2.isHidden
    view1.layer.removeAllAnimations()
    view2.layer.removeAllAnimations()
    view.layer.removeAllAnimations()
    stackView.layer.removeAllAnimations()
    // after stopping animation the values are unpredictable, so set values to old
    view1.isHidden = oldHideView1 //    <- Solution is here
    view2.isHidden = oldHideView2 //    <- Solution is here

    UIView.animate(withDuration: 0.3,
                   delay: 0.0,
                   usingSpringWithDamping: 0.9,
                   initialSpringVelocity: 1,
                   options: [],
                   animations: {
                    view1.isHidden = hideView1
                    view2.isHidden = hideView2
                    stackView.layoutIfNeeded()
    },
                   completion: nil)
}

の回答解勤務しました。いる課題に対する取り組みこのように思わからない場合は正しい道?), らないといけなかったので問題を呼び出す時にも高速時に前回アニメーションは未だ終了しました).していただくために私たアニメーション customAnim ブロックです。

extension UIView
{

    func niceCustomTranstion(
        duration: CGFloat = 0.3,
        options: UIView.AnimationOptions = .transitionCrossDissolve,
        customAnim: @escaping () -> Void
        )
    {
        UIView.transition(
            with: self,
            duration: TimeInterval(duration),
            options: options,
            animations: {
                customAnim()
        },
            completion: { (finished) in
                if !finished
                {
                    // NOTE: This fixes possible flickering ON FAST TAPPINGS
                    // NOTE: This fixes possible flickering ON FAST TAPPINGS
                    // NOTE: This fixes possible flickering ON FAST TAPPINGS
                    self.layer.removeAllAnimations()
                    customAnim()
                }
        })

    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top