因此,我正在尝试制作一款游戏,用户必须尽可能快地向下滚动才能逃离无限变大的“方 block ”。这是我的问题:我使用 UI ScrollView 作为我的机制,以普通 UI View 作为 subview 进行滚动。我有一个时间设置为每 0.005 秒触发一次,这会增加“ block ”的高度和 ScrollView 的内容高度(以便用户在技术上可以无限滚动)。问题是每当用户开始滚动时,它就会停止触发(即 block 停止变高)。但是,当用户停止滚动时,一切都会立即恢复正常。这是我拥有的所有代码:
- (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);
}
任何关于 block 停止增长的原因的帮助都会很棒!对于这个具体/简单的问题,我深表歉意,我对这个网站有些陌生,我只是在网上四处寻找解决这个问题的帮助。
最佳答案
您应该将计时器添加到另一种循环模式:
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)
关于ios - 在 UIScrollView 中滚动时动画停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32835858/