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:nil afterDelay: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屋!