我正在研究一些类似自动收报机的功能,并且正在使用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];
}
Run Code Online (Sandbox Code Playgroud)
这适用于滚动视图,动画发生在集合视图中.但是,实际上只渲染动画结尾处可见的单元格.cellForItemAtIndexPath调用contentOffset不会导致调用.当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 …Run Code Online (Sandbox Code Playgroud) 我有一个UICollectionView可以添加和删除的单元格.我正在使用这些更改performBatchUpdates,并且布局按预期动画.
当我滚动到内容的末尾并删除一个项目以使contentSize减少时出现问题:这会导致contentOffset更改,但更改不是动画.相反,contentOffset删除动画完成后立即跳转.我已经尝试手动更新contentOffset删除旁边,但这对我也不起作用.
我正在使用自定义布局,但我使用以下代码看到标准流布局的相同行为:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.items.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [self.items objectAtIndex:indexPath.item];
return cell;
}
- (IBAction)addItem:(UIBarButtonItem *)sender
{
self.runningCount++;
[self.items addObject:[NSString stringWithFormat:@"Item %u",self.runningCount]];
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.items.count-1 inSection:0]]];
} completion:nil];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{ …Run Code Online (Sandbox Code Playgroud)