why changing of tab, stopping the animation in ios app?
Asked Answered
C

1

9

i have multiple tabs in my app. i one view controller there is animation running, when i switch to another view controller, and again comes to view controller with animation, then animation stops,

can anybody guide me to work my Xcode even switching of tab in Iphone app?

- (IBAction)btn:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *soundStatus = [defaults objectForKey:@"Sound_Swich"];
    //NSLog(@"soundstatus is %@ ", soundStatus);
    if([soundStatus isEqual:@"YES"])
    {
        [self soundEffect];
    }
    if ([btnpress isEqualToString:@"start"]) {

        btnpress= @"pause";

        int radius = 100;
        circle = [CAShapeLayer layer];

        circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
                                                 cornerRadius:radius].CGPath;

        circle.fillColor = [UIColor clearColor].CGColor;
        circle.strokeColor = [UIColor greenColor].CGColor;

        circle.lineWidth = 5;
        [imgview.layer addSublayer:circle];



        // Configure animation
         drawAnimation= [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        drawAnimation.duration            = 5.0; // "animate over 10 seconds or so.."
        drawAnimation.repeatCount         = 8.0;  // Animate only once..
        // Animate from no part of the stroke being drawn to the entire stroke being drawn
        drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];

        // Experiment with timing to get the appearence to look the way you want
        drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

        // Add the animation to the circle
        [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
        timerWasStarted=YES;
        //totalSeconds = 120;
        totalSeconds = 10;
        twoMinTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                       target:self
                                                     selector:@selector(timer)
                                                     userInfo:nil
                                                      repeats:YES];

       // NSLog(@"start %@",[NSDate date]);
    }// to pause animation and timer
    else if ([btnpress isEqualToString:@"pause"]){
        flag=1;
        btnpress=@"resume";
        pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];
        previousFireDate = [twoMinTimer fireDate];
        [twoMinTimer setFireDate:[NSDate distantFuture]];
       // NSLog(@"pause %@",[NSDate date]);
        [self pauseLayer:circle];


    }// to resume animation and timer

    else if([btnpress isEqual:@"resume"]&&flag==1){
        btnpress= @"pause";
        float pauseTime = -1*[pauseStart timeIntervalSinceNow];
        [twoMinTimer setFireDate:[NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate]];
        [self resumeLayer:circle];
    }

    - (void)pauseLayer:(CALayer *)layer
    {

        pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
        layer.speed = 0.0;
        layer.timeOffset = pausedTime;
        //NSLog(@"pauseoffset %f",pausedTime);
    }

    - (void)resumeLayer:(CALayer *)layer
    {
       // NSLog(@"resume %@",[NSDate date]);

        CFTimeInterval pausedTime1 = [layer timeOffset];
        layer.speed = 1.0;
        layer.timeOffset = 0.0;
        layer.beginTime = 0.0;
        timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime1;
        layer.beginTime = timeSincePause;

        //NSLog(@"resume offset %f",[layer timeOffset]);
    }
Craig answered 8/9, 2014 at 11:38 Comment(10)
is viewWillAppear: method is there in the animation tab?Whicker
even writing code in viewWillAppear stops the animation,Craig
whenever u change the tab and the come back to the same tab, the viewWillAppear: method will be called and the view get backs to it normal form.Whicker
but variable of animations not maintaining there values, check this code if u get leak point.Craig
i am try to add code , but due to less reputation i m unable, sorry, can u give me reputation,Craig
post ur code by editing the questionWhicker
@Whicker did u see code.?Craig
where have u posted the code? @Jagveer SinghWhicker
can u accept my question?Craig
this is working code sirCraig
C
23

answer i needed was

 drawAnimation.removedOnCompletion=NO;
Craig answered 27/9, 2014 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.