在UIScrollView中滚动时动画停止 [英] Animation stops when scrolling in UIScrollView

查看:553
本文介绍了在UIScrollView中滚动时动画停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试制作一个游戏,其中用户必须尽可能快地向下滚动才能逃脱无限增大的方块。这是我的问题:我使用UI ScrollView作为将普通UI View作为子视图进行滚动的机制。我将时间设置为每0.005秒触发一次,这会同时增加块的高度和滚动视图的内容高度(以便用户可以从技术上无限滚动)。问题在于,每当用户开始滚动时,这都会停止触发(即,方块不再变高)。但是,当用户停止滚动时,所有内容立即都可以正常工作。这是我拥有的所有代码:

So I am trying to make a game where the user must scroll down as fast as they can in order to escape a "block" that grows infinitely bigger. Here is my problem: I am using a UI ScrollView as my mechanism for scrolling with a normal UI View as a subview. I have a time set to fire every 0.005 seconds that increases the height of both the "block" and the content height of the scroll view (so that the user can technically scroll infinitely). The problem is that whenever the user begins to scroll, this stops firing (i.e. the block stops getting taller). However, immediately when the user stops scrolling everything again works as it should. Here is all of the code that I have:

- (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);
}

任何有关阻止块停止增长的帮助都很棒!抱歉,这个问题很简单,我是这个网站的新手,我只是在网上寻找有关此问题的帮助。

Any help as to why the block stops growing would be great! Sorry for the specific/simple question, I am somewhat new to this site and I am just looking around online for some help with this problem.

推荐答案

您应该将计时器添加到其他循环模式:

You should add timer to another loop mode:

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

已为Swift进行了更新:

Updated for Swift:

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

这篇关于在UIScrollView中滚动时动画停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆