两张图片之间碰撞计数器的问题

3

这是我的代码:

-(void)collision {

    if(CGRectIntersectsRect(imageView.frame,centre.frame)){

        [imageView removeFromSuperview];
        count++;
        label.text= [NSString stringWithFormat:@"%d", count];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES];

    count=0;    
    scale=1;
}

使用这段代码后,我的计数增加了一个以上,有时是34,有时是74或70,为什么?

为什么是 count==0?这是错误吗?试试 count=0。另外使用 ++something 而不是 something++,它更快。 - HiTECNOLOGYs
1个回答

1

为什么要使无效,我不想停止我的计时器? - bernard langue
@bernard Langue,好的,我不是iOS多线程方面的专家。但是你不能停止计时器,视图也不能立即被删除。另外,当图片被移除时,没有很好的理由去每秒调用选择器一百次。我的建议是仅在需要使用时启动计时器,并在使用后立即停止它。我相信也可以使用几个计时器。 - A-Live
我该如何做到“仅在需要使用时启动计时器,并立即停止它”? - bernard langue
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES]; 这个会启动定时器,- (void)invalidate 会停止它,是吗?开始/停止的时间点取决于您应用程序的行为,如果您需要让计时器继续工作,可以在删除视图之前尝试使用计数器设置标签文本,希望能帮到您。 - A-Live

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接