UIView包含NSTimer - 不发布

时间:2016-02-08 10:40:12

标签: objective-c uiview nstimer cadisplaylink

我正在使用一个具有一些CAAnimation的UIView子类,该子类将在从另一个类执行后执行。在UIView类中,我使用CADisplayLink和NSTimer来增加动画中使用的CATextLayer。每当我在运行动画后回到上一个视图控制器时,它就会继续在后台运行。当我转到动画播放的View控制器时,它会显示错误的计数。我的代码有什么问题吗?

在.h文件中,

@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;

在.m文件中 //下面的代码在方法

中调用
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];
    }

在sum1方法中,

-(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];

}

为了实现缓出功能,我使用75%的增量快速和25%增量慢,这在需求中提到。

-(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];
    }
}

即使我使计时器无效,此计时器仍会继续运行,从dealloc()方法内的superview中删除视图。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

最后,通过简单的方法找到答案。在超级视图中创建uiview子视图的IBOutlet,并在执行segue时使那里的NSTimer无效。