【发布时间】:2014-04-18 20:28:38
【问题描述】:
我正在开发一些类似自动收报机的功能,并且正在使用UICollectionView。它最初是一个 scrollView,但我们认为 collectionView 将更容易添加/删除单元格。
我正在使用以下内容为 collectionView 设置动画:
- (void)beginAnimation {
[UIView animateWithDuration:((self.collectionView.collectionViewLayout.collectionViewContentSize.width - self.collectionView.contentOffset.x) / 75) delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionRepeat | UIViewAnimationOptionBeginFromCurrentState) animations:^{
self.collectionView.contentOffset = CGPointMake(self.collectionView.collectionViewLayout.collectionViewContentSize.width, 0);
} completion:nil];
}
这适用于滚动视图,并且动画正在与集合视图一起发生。但是,只有在动画结束时可见的单元格才会被实际渲染。调整 contentOffset 不会导致调用 cellForItemAtIndexPath。当 contentOffset 发生变化时,如何让单元格呈现?
编辑: 更多参考(不确定是否有很大帮助):
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TickerElementCell *cell = (TickerElementCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"TickerElementCell" forIndexPath:indexPath];
cell.ticker = [self.fetchedResultsController objectAtIndexPath:indexPath];
return cell;
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// ...
[self loadTicker];
}
- (void)loadTicker {
// ...
if (self.animating) {
[self updateAnimation];
}
else {
[self beginAnimation];
}
}
- (void)beginAnimation {
if (self.animating) {
[self endAnimation];
}
if ([self.tickerElements count] && !self.animating && !self.paused) {
self.animating = YES;
self.collectionView.contentOffset = CGPointMake(1, 0);
[UIView animateWithDuration:((self.collectionView.collectionViewLayout.collectionViewContentSize.width - self.collectionView.contentOffset.x) / 75) delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionBeginFromCurrentState) animations:^{
self.collectionView.contentOffset = CGPointMake(self.collectionView.collectionViewLayout.collectionViewContentSize.width, 0);
} completion:nil];
}
}
【问题讨论】:
-
您使用
animateWithDuration:...更改滚动有什么特别的原因吗?有一个内置方法scrollToItemAtIndexPath:atScrollPosition:animated:。 -
另外,您能否展示
collectionView:cellForItemAtIndexPath的代码以及当前正在发生的事情的视频? (您可以使用 QuickTime 播放器录制模拟器)。 -
@Fogmeister 我没有使用
scrollToItemAtIndexPath:atScrollPosition:animated:,因为我无法设置持续时间。我想要一个滚动的股票... -
你的意思是像一排图像或在屏幕上不断滚动的东西?类似的东西?
-
你从来没有回答我的问题。
标签: ios animation uicollectionview contentoffset