UIActivityIndicatorView在UICollectionViewCell中不旋转 [英] UIActivityIndicatorView not spinning in UICollectionViewCell
问题描述
我在集合视图的底部加载了一个自定义单元格.它的唯一工作是显示活动指示器视图-在应用程序进行新的工作呼叫时发生.
I have a custom cell that is loaded at the bottom of my collection view. Its only job is to display an activity indicator view - which happens while the app is making a new work call.
所以我将其添加到单元格中,如下所示:
So I added it to the cell like so:
BBLoaderCell *loaderCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoaderCell" forIndexPath:indexPath];
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.hidden = NO;
activityIndicator.center = loaderCell.imageView.center;
activityIndicator.tag = 10;
[loaderCell.imageView addSubview:activityIndicator];
[activityIndicator startAnimating];
return loaderCell;
这会在我视图的最后一个单元格中显示一个活动指示器-但是它不会旋转.有什么想法吗?
This shows an activity indicator in the last cell of my view - however it does not spin. Any ideas?
找到解决方案
根据评论-这是一个线程问题.我还建议将代码移到该单元格的自定义nib文件中.
As per comments - it was a threading issue. I also took the suggestion to move the code to the custom nib file for the cell.
这是重构的代码:
BBLoaderCell *loaderCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoaderCell" forIndexPath:indexPath];
loaderCell.backgroundColor = [UIColor clearColor];
if (self.loadingMore == NO){
dispatch_async(dispatch_get_main_queue(), ^{
activityIndicator.hidden = YES;
[loaderCell.spinner stopAnimating];
});
}
else if (self.loadingMore == YES){
dispatch_async(dispatch_get_main_queue(), ^{
activityIndicator.hidden = NO;
[loaderCell.spinner startAnimating];
});
}
return loaderCell;
这是我需要的.
谢谢大家!
推荐答案
我认为这可能与线程化有关(例如,主线程上的UI尚未准备好对指示器进行动画处理).您可以尝试执行以下操作:
I think this might have something to do with threading (eg. the UI on main thread not being ready to animate the indicator). You could try something like the following:
[activityIndicator performSelector:@selector(startAnimating :) withObject:n after afterlay:1];
这只是一个可能的建议,您应该尝试不同的变体,并检查哪个变体适时地将动画正确地施加到主线程上.另一种变化:
This is just a possible suggestion though, you should try different variations and check which one forces the animation onto the main thread correctly and in a timely matter. Another variation:
dispatch_async(dispatch_get_main_queue(), ^{
[activityIndicator startAnimating];
});
这篇关于UIActivityIndicatorView在UICollectionViewCell中不旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!