【问题标题】:Animation stops when scrolling in UIScrollView在 UIScrollView 中滚动时动画停止
【发布时间】:2015-12-26 10:54:44
【问题描述】:

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

任何关于为什么块停止增长的帮助都会很棒!对于这个具体/简单的问题,我很抱歉,我对这个网站有点陌生,我只是在网上四处寻找解决这个问题的帮助。

【问题讨论】:

    标签: ios animation uiscrollview nstimer


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 我无法理解有关模式的文档,但这实际上解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2014-10-10
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多