因此,我试图制作一款游戏,用户必须尽可能快地向下滚动,才能逃脱无限增大的“块”。这是我的问题:我使用 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];
更新为 Swift:
let timer = Timer.scheduledTimer(timeInterval: 0.005, target: self, selector: #selector(increaseDarknessHeight), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: .common)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)