0

I'm using one UIView subclass that has some CAAnimation which would be performed after performsegue from another class. In the UIView class, I'm using CADisplayLink and NSTimer to increment the CATextLayer used in the animation. Whenever, I came back to the previous view controller after running the animation, it keeps on running in the background. When I go to View controller where the animation is playing, it shows me incorrect count. Is there anything wrong with my code ?

In the .h file,

@property (assign,nonatomic) CFTimeInterval duration;
@property (nonatomic,strong) NSTimer *tim1;
@property (nonatomic,strong) NSTimer *tim2;
@property (nonatomic,strong) NSTimer *tim3;
@property (nonatomic,strong) NSTimer *startup;
@property (nonatomic,strong) NSTimer *starter;
@property (nonatomic,strong) NSTimer *suspender;
@property (nonatomic,strong) NSTimer *humansum;
@property (nonatomic,strong) NSTimer *transportsum;
@property (nonatomic,strong) NSTimer *wastesum;
@property (nonatomic,strong) NSTimer *watersum;
@property (nonatomic,strong) NSTimer *energysum;
@property (nonatomic,strong) NSTimer *humanslowsum;
@property (nonatomic,strong) NSTimer *transportslowsum;
@property (nonatomic,strong) NSTimer *wasteslowsum;
@property (nonatomic,strong) NSTimer *waterslowsum;
@property (nonatomic,strong) NSTimer *energyslowsum;
@property (nonatomic,strong) CADisplayLink  *displayLink;

In the .m file, //Below code is called inside a method

displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

- (void)tick:(CADisplayLink *)sender
{
    duration = sender.duration;
 startup=[NSTimer scheduledTimerWithTimeInterval:(2+(0.05*duration)) target:self selector:@selector(calltheanimating_function) userInfo:nil repeats:NO];

 if(initil==YES){
        //
            humansum  =[NSTimer scheduledTimerWithTimeInterval:(15.4+(0.05*duration)) target:self selector:@selector(sum1) userInfo:nil repeats:YES];
    }
    else{
        humansum  =[NSTimer scheduledTimerWithTimeInterval:13.4 target:self selector:@selector(sum1) userInfo:nil repeats:YES];
    }

In the sum1 method,

-(void)sum1{
    NSLog(@"Hvalue %f",humanvalue);
    num11=humanvalue;//
    CATextLayer *humanscore=self.layers[@"humanscore"];
    humanscore.string=@"0";
    actualtext=0;
    tmep=0;
        humanslowsum =[NSTimer scheduledTimerWithTimeInterval:(0.7/(0.7*num11)) target:self selector:@selector(final1) userInfo:nil repeats:YES];

}

For implementing ease out function, I'm using 75% of the increment as fast and 25% increment as slow which is mentioned in the requirement.

-(void)final1{
    if(tmep<round(num11)){
        tmep++;
        CATextLayer *t=self.layers[@"stepscore"];
        int m=[t.string intValue];
        m++;
        t.string=[NSString stringWithFormat:@"%d",m];
        CATextLayer *humanscore=self.layers[@"humanscore"];
        actualtext++;
        humanscore.string=[NSString stringWithFormat:@"%d",actualtext];
        NSLog(@"%d",m);
    }
    else{
        [humanslowsum  invalidate];
    }
}

This timer keeps running even though if I invalidate the timer, remove the view from the superview inside dealloc() method. Please help me with the issue.

1 Answers1

0

Finally, found an answer by simple method. Creating an IBOutlet of the uiview subview inside super view and invalidating the NSTimer(s) over there while performing segue.