在UI ScrollView中滚动时动画停止

因此,我试图制作一个游戏,用户必须尽可能快地向下滚动,以逃避一个无限大的“块”。 这是我的问题:我使用UI ScrollView作为我的机制,使用普通的UI视图作为子视图进行滚动。 我有一个时间设置为每隔0.005秒触发一次,增加“块”的高度和滚动视图的内容高度(这样用户可以在技术上无限滚动)。 问题在于,无论何时用户开始滚动,这都会停止发射(即阻止停止变高)。 但是,当用户停止滚动时,立即再次按预期工作。 这是我的所有代码:

- (void)viewDidLoad { [super viewDidLoad]; self.mainScrollView.delegate = self; self.mainScrollView.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height * 2); self.mainScrollView.backgroundColor = [UIColor greenColor]; self.mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 10); self.mainScrollView.scrollEnabled = YES; self.darknessBlock.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 100); self.darknessBlock.backgroundColor = [UIColor blueColor]; [NSTimer scheduledTimerWithTimeInterval:0.005 target:self selector:@selector(increaseDarknessHeight) userInfo:nil repeats:YES]; } -(void)increaseDarknessHeight{ int newHeight = self.darknessBlock.frame.size.height + 1; self.darknessBlock.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.darknessBlock.frame.size.width, newHeight); } 

任何帮助,为什么块停止增长将是伟大的! 对不起特定/简单的问题,我对这个网站有些新意,我只是在网上寻找一些帮助来解决这个问题。

您应该将计时器添加到另一个循环模式:

 NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.005 target:self selector:@selector(increaseDarknessHeight) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];