रद्द एक UIView एनीमेशन?

वोट
221

यह एक रद्द करने के लिए संभव है UIViewएनीमेशन, जबकि यह कार्य प्रगति पर है? या मैं सीए के स्तर के लिए छोड़ करना होगा?

यानी मैं इस तरह (शायद भी समाप्त एनीमेशन कार्रवाई की स्थापना) कुछ किया है:

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

// set view properties

[UIView commitAnimations];

लेकिन इससे पहले कि एनीमेशन पूरा करता है और मैं एनीमेशन समाप्त हो गया घटना मिलता है, मैं इसे (इसे काट कम) रद्द करना चाहते हैं। क्या यह संभव है? और एक या दो लोग अटकलें है कि यह नहीं किया जा सकता - चारों ओर Googling कुछ लोगों के जवाब के साथ एक ही प्रश्न पूछ पाता है।

17/02/2009 को 00:19
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


17 जवाब

वोट
342

उपयोग:

#import <QuartzCore/QuartzCore.h>

.......

[myView.layer removeAllAnimations];
19/03/2010 को 07:53
का स्रोत उपयोगकर्ता

वोट
111

जिस तरह से मैं यह कर अपना अंतिम बिंदु के लिए एक नया एनीमेशन बनाने के लिए है। एक बहुत ही कम अवधि निर्धारित करें और आप का उपयोग सुनिश्चित करें कि +setAnimationBeginsFromCurrentState:विधि वर्तमान स्थिति से शुरू करने के लिए। जब आप यह हाँ करने के लिए सेट, वर्तमान एनीमेशन ही समाप्त हो जाता है। इस तरह दिखता है:

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

// set view properties

[UIView commitAnimations];
08/05/2009 को 22:46
का स्रोत उपयोगकर्ता

वोट
61

को रोकने के लिए सबसे आसान तरीका है सब एक विशेष दृश्य की एनिमेशन, तुरंत , यह है:

QuartzCore.framework करने के लिए परियोजना को लिंक करें। अपने कोड के शुरू में:

#import <QuartzCore/QuartzCore.h>

अब, जब आप उनके पटरियों में एक दृश्य के मृत पर सभी एनिमेशन बंद करना चाहते हैं, इस कहते हैं:

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

मध्य लाइन से ही सभी काम करेगा, लेकिन वहाँ runloop खत्म होने तक एक देरी ( "पल पुनः बनाने") है। कि देरी को रोकने के लिए एक स्पष्ट लेनदेन ब्लॉक में आदेश लपेट दिखाया गया है। कोई अन्य बदलाव प्रदान की यह काम करता है वर्तमान runloop में इस स्तर पर प्रदर्शन किया गया है।

04/01/2012 को 00:03
का स्रोत उपयोगकर्ता

वोट
43

आईओएस 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;
                     }
                 }];
}
11/08/2011 को 09:30
का स्रोत उपयोगकर्ता

वोट
39

एक एनीमेशन आप बस संपत्ति है कि वर्तमान में UIView एनीमेशन के बाहर एनिमेटेड जा रहा है, निर्धारित करने की आवश्यकता को रद्द करने के। यही कारण है कि एनीमेशन बंद हो जाएगा जहाँ भी यह है, और UIView सेटिंग तुम सिर्फ परिभाषित पर चला जाएगा।

19/06/2010 को 18:09
का स्रोत उपयोगकर्ता

वोट
9

iOS 8 पर काम आप अन्य तरीकों के एक दृश्य के संपत्ति से कोई भी के बजाय लगातार बदल कर एक बाधा एनिमेट रहे हैं।

उदाहरण एनीमेशन:

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];
}
07/11/2014 को 17:21
का स्रोत उपयोगकर्ता

वोट
8

इस उत्तर के फिर से शुरू करने क्षमा करें, लेकिन आईओएस में 10 बातें थोड़े बदल गया है, और अब रद्द संभव है और तुम भी शान से रद्द कर सकते हैं!

आईओएस 10 के बाद आप के साथ एनिमेशन रद्द कर सकते हैं UIViewPropertyAnimator !

UIViewPropertyAnimator(duration: 2, dampingRatio: 0.4, animations: {
    view.backgroundColor = .blue
})
animator.stopAnimation(true)

आप सच पार कर लेते हैं यह एनीमेशन रद्द और यह सही बंद हो जाता है, जहां आप इसे रद्द कर दिया। पूरा होने विधि कहा जाता है नहीं किया जाएगा। हालांकि, अगर आप गलत पारित आप एनीमेशन परिष्करण के लिए जिम्मेदार हैं:

animator.finishAnimation(.start)

आप अपने एनीमेशन समाप्त कर सकते हैं और वर्तमान स्थिति (.current) में रहने या प्रारंभिक अवस्था (.start) के लिए जाने या अंत राज्य के लिए (.end)

वैसे, आप भी रोक सकते हैं और बाद में पुन: प्रारंभ कर सकते हैं ...

animator.pauseAnimation()
animator.startAnimation()

नोट: यदि आप रद्द आप अपने एनीमेशन रिवर्स या यहाँ तक कि अपने एनीमेशन बदल आप इसे थामने के बाद कर सकते हैं अचानक नहीं करना चाहते हैं!

09/11/2017 को 17:57
का स्रोत उपयोगकर्ता

वोट
7

तुम सिर्फ रोकना चाहते हैं / सुचारू रूप से एनीमेशन रोक

self.yourView.layer.speed = 0;

स्रोत: एक परत के पेड़ के एनीमेशन को रोकने के लिए कैसे

29/06/2015 को 11:02
का स्रोत उपयोगकर्ता

वोट
5
[UIView setAnimationsEnabled:NO];
// your code here
[UIView setAnimationsEnabled:YES];
06/02/2015 को 05:30
का स्रोत उपयोगकर्ता

वोट
5

उपरोक्त में से कोई मेरे लिए यह हल है, लेकिन इस मदद की: UIViewएनीमेशन संपत्ति तुरंत सेट है, तो यह एनिमेट। यह एनिमेशन बंद हो जब प्रस्तुति परत मॉडल (सेट संपत्ति) से मेल खाता है।

मैं अपने मुद्दे को, जो था "मैं जहां आप दिखाई देते हैं की तरह लग रहे से चेतन करना चाहते हैं" हल ( 'आप' दृश्य अर्थ)। यदि आप चाहते हैं कि, तो:

  1. QuartzCore जोड़ें।
  2. CALayer * pLayer = theView.layer.presentationLayer;

प्रस्तुति परत को स्थिति सेट

मैं सहित कुछ विकल्पों का उपयोग UIViewAnimationOptionOverrideInheritedDuration

लेकिन क्योंकि एप्पल के प्रलेखन अस्पष्ट है, मैं जब इस्तेमाल किया है, तो यह वास्तव में अन्य एनिमेशन ओवरराइड करता है पता नहीं है, या सिर्फ टाइमर रीसेट करता है।

[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) {}];

और वह अब के लिए एक सभ्य समाधान होना चाहिए।

16/08/2011 को 18:21
का स्रोत उपयोगकर्ता

वोट
1

मेरी भी यही समस्या है; एपीआई कुछ विशिष्ट एनीमेशन रद्द करने के लिए कुछ भी नहीं है।

+ (void)setAnimationsEnabled:(BOOL)enabled

सभी एनिमेशन अक्षम करता है, और इस तरह मेरे लिए काम नहीं करता। वहाँ दो समाधान है:

1) अपने एनिमेटेड एक subview वस्तु बनाते हैं। तब, जब आप उस दृश्य के लिए एनिमेशन रद्द करना चाहते हैं, दृश्य हटाने या इसे छिपाने। बहुत ही सरल है, लेकिन आप एनिमेशन के बिना subview पुन: बनाने के लिए अगर आप ध्यान में रखते हुए इसे रखने की आवश्यकता की जरूरत है।

2) anim केवल एक दोहराने, और अगर इस तरह, जरूरत anim पुनः आरंभ करने के लिए एक प्रतिनिधि चयनकर्ता बनाना:

-(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 के साथ समस्या) है कि वहाँ हमेशा anim रोक देरी है के रूप में यह वर्तमान एनीमेशन चक्र खत्म है।

उम्मीद है की यह मदद करेगा।

08/05/2009 को 18:03
का स्रोत उपयोगकर्ता

वोट
0

जवाब समाधान में से कोई भी मेरे लिए काम किया। मैं अपने मुद्दों इस तरह (अगर यह एक सही तरीका है मैं नहीं जानता?) हल क्योंकि मैं समस्या नहीं थी जब बुला यह भी तेजी से (जब पिछला एनीमेशन अभी तक समाप्त नहीं था)। मैं के साथ मेरी वांछित एनीमेशन पारित 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()
                }
        })

    }

}
20/08/2019 को 14:52
का स्रोत उपयोगकर्ता

वोट
0

जब मैं के साथ काम UIStackViewएनीमेशन, के अलावा removeAllAnimations()एक प्रारंभिक क्योंकि कुछ मूल्यों को निर्धारित करने की आवश्यकता removeAllAnimations()के लिए उन्हें अप्रत्याशित राज्य के लिए सेट कर सकते हैं। मैं stackViewके साथ view1और view2अंदर, और एक दृश्य दिखाई देना चाहिए और एक छिपा हुआ:

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)
}
07/02/2019 को 09:03
का स्रोत उपयोगकर्ता

वोट
0

स्टीफन Darlington के समाधान की स्विफ्ट संस्करण

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

// set view properties
UIView.commitAnimations()
26/12/2017 को 15:09
का स्रोत उपयोगकर्ता

वोट
0

मूल या अंतिम करने के लिए राज्य को वापस लाने में बिना एक एनीमेशन रोकने के लिए:

CFTimeInterval paused_time = [myView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
myView.layer.speed = 0.0;
myView.layer.timeOffset = paused_time;
03/12/2015 को 21:43
का स्रोत उपयोगकर्ता

वोट
0
CALayer * pLayer = self.layer.presentationLayer;
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.001 animations:^{
    self.frame = pLayer.frame;
}];
28/01/2013 को 15:54
का स्रोत उपयोगकर्ता

वोट
0

यहां तक कि अगर आप एनीमेशन ऊपर तरीकों से एनीमेशन रद्द didStopSelectorअभी भी चलाता है। तो अगर आप समस्या है अगर आप एनिमेशन द्वारा संचालित अपने आवेदन में तर्क राज्यों की है। ऊपर वर्णित मैं के संदर्भ चर का उपयोग तरीके के साथ इस कारण से UIViewएनिमेशन। आप एनीमेशन के संदर्भ परम, द्वारा अपने कार्यक्रम की वर्तमान स्थिति के पार कर लेते हैं एनिमेशन बंद हो जब अपने didStopSelectorही वह कुछ करना चाहिए या सिर्फ वर्तमान स्थिति और संदर्भ के रूप में पारित राज्य मूल्य के आधार पर लौटने समारोह तय कर सकते हैं।

05/10/2010 को 09:52
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more