0

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

在 .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() 方法中的超级视图中删除视图。请帮我解决这个问题。

4

1 回答 1

0

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

于 2016-02-08T12:37:36.590 回答