ios - 在 UIScrollView 中滚动时动画停止

标签 ios animation uiscrollview nstimer

因此,我正在尝试制作一款游戏,用户必须尽可能快地向下滚动才能逃离无限变大的“方 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/

相关文章:

ios - Apple 开发者 Fairplay 流媒体证书到期

Android如何获取slide_in_right.xml动画

iphone - 正确缩放包含许多 subview 的 UIScrollView

ios - 我无法将屏幕截图上传到新的 iTunes Connect 媒体管理器

ios - 如何在 Swift (iOS) 中使用 MKSnapshotter

ios - 从我的应用程序打开 Facebook 应用程序

android - 如何以编程方式为自定义 View (ImageView) 设置动画?

ios - 在 iOS 中暂停动画( Objective-C )

ios - UIImageView -> UIScrollview,裁剪和裁剪 ScrollView 坐标

ios - 如何在按钮操作上显示图像