在UIScrollView中滚动时动画停止

时间:2015-09-29 04:46:46

标签: ios animation uiscrollview nstimer

所以我试图制作一个用户必须尽可能快地向下滚动的游戏,以逃避一个无限大的“块”。这是我的问题:我使用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);
}

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

1 个答案:

答案 0 :(得分:8)

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

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

更新了Swift:

let timer = Timer.scheduledTimer(timeInterval: 0.005, target: self, selector: #selector(increaseDarknessHeight), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: .common)