gpt4 book ai didi

ios - UICollectionVIew:滚动时动画单元格

转载 作者:IT王子 更新时间:2023-10-29 08:02:48 26 4
gpt4 key购买 nike

我希望我的 UICollectionView 中的项目能够在用户滚动列表时进行动画查看(我使用的是 UICollectionViewFlowLayout 的子类)。

我在布局管理器中指定位置,我希望能够做的是还指定一个初始变换,并在正确的时间(当单元格首次出现在屏幕上时)将其应用到动画中。要查看我的意思,请查看 iOS 上的 Google Plus 应用程序。理想情况下,根据单元格的位置进行不同的转换。

我似乎无法找到一种方法来确定何时显示一个单元格(没有 UITableView 上的 willDisplayCell 等价物)或任何关于显示位置的指针为此。

有什么建议吗?

在这个屏幕截图中,您几乎可以看出 Google Plus 中的动画: Example in the Google Plus App

另外,看看 iPad 上的 iPhoto。我不知道他们是否在使用 UIcollectionView(可能不是,因为它在 iOS5 上工作)但这是我正在寻找的那种效果,照片似乎是从右边飞进来的。

最佳答案

您可以独立于自定义布局实现此效果。

动画代码应该放在collectionView:cellForItemAtIndexPath:

当一个单元格出列时,它的 frame 将被设置为在您的布局中指定的内容。您可以将其存储在临时变量中作为单元格的最终 frame。然后,您可以将 cell.frame 设置为屏幕外的初始位置,然后朝着所需的最终位置设置动画。类似的东西:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

UICollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

CGRect finalCellFrame = cell.frame;
//check the scrolling direction to verify from which side of the screen the cell should come.
CGPoint translation = [collectionView.panGestureRecognizer translationInView:collectionView.superview];
if (translation.x > 0) {
cell.frame = CGRectMake(finalCellFrame.origin.x - 1000, - 500.0f, 0, 0);
} else {
cell.frame = CGRectMake(finalCellFrame.origin.x + 1000, - 500.0f, 0, 0);
}

[UIView animateWithDuration:0.5f animations:^(void){
cell.frame = finalCellFrame;
}];

return cell;
}

上面的代码将为水平 UICollectionView 上的单元格设置动画,来自屏幕顶部和两侧,具体取决于滚动方向。您还可以检查当前的 indexPath,让单元格在更复杂的布局中从不同位置开始动画,以及与框架一起为其他属性设置动画,或者应用变换。

关于ios - UICollectionVIew:滚动时动画单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17764991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com