如何使用流畅的动画垂直地为集合视图设置动画
Posted
技术标签:
【中文标题】如何使用流畅的动画垂直地为集合视图设置动画【英文标题】:How to animate collection view vertically with smooth animation 【发布时间】:2017-08-23 07:23:19 【问题描述】:任何人都可以回答一些示例代码以将 uicollectionview 垂直设置到底部。
dispatch_async(dispatch_get_main_queue(), ^
[self.clViewImages reloadData];
scrolling collection view to bottom
NSInteger section = [self numberOfSectionsInCollectionView:self.clViewImages] - 1;
NSInteger item = [self collectionView:self.clViewImages numberOfItemsInSection:section] - 1;
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
[self.clViewImages scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
);
这会非常快速地滚动我的收藏视图。我想明智地软动画部分。 帮我看看。
提前致谢
【问题讨论】:
您可以一次性滚动收藏视图中的所有项目,这就是它快速的原因。因此,您可以一次滚动到屏幕上可见的 n 个项目(4-5 个项目),然后滚动到下一个 n 项目,依此类推。这样我们就平滑了。 好的 @RahulKumar 让我这样做 【参考方案1】:首先,请确保您设置了您的集合视图数据源和委托,如下所示。
collectionView.datasource = self;
collectionView.delegate = self;
接下来你必须检查集合数组不应该为零。
如果所有条件都匹配,请尝试重新加载您的收藏视图。
[collectionView reloadData];
【讨论】:
Neha 一切正常。问题是我正在重新加载集合视图,同时将其动画到底部,而且速度太快了。有什么解决办法吗???【参考方案2】:这对我有帮助 - :
@property (nonatomic, assign) CGPoint scrollingPoint, endPoint;
@property (nonatomic, strong) NSTimer *scrollingTimer;
@property (nonatomic, assign) BOOL animationStarted;
//scroll with finite time interval
- (void)scrollSlowlyWithInterval:(NSNumber*)interval
CGFloat height = self.clViewImages.contentSize.height;
animationStarted = YES;
// Set the point where the scrolling stops.
self.endPoint = CGPointMake(0, height-self.clViewImages.frame.size.height);
// Assuming that you are starting at 0, 0 and scrolling along the x-axis.
self.scrollingPoint = CGPointMake(0, 0);
// Change the timer interval for speed regulation.
self.scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:[interval doubleValue] target:self selector:@selector(scrollSlowlyToPoint) userInfo:nil repeats:YES];
- (void)scrollSlowlyToPoint
[self.clViewImages setContentOffset:self.scrollingPoint animated:YES];
// Here you have to respond to user interactions or else the scrolling will not stop until it reaches the endPoint.
if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint))
[self.scrollingTimer invalidate];
animationStarted = NO;
// Going one pixel to the right.
CGFloat height = [UIScreen mainScreen].bounds.size.height/2;
self.scrollingPoint = CGPointMake(self.scrollingPoint.x, self.scrollingPoint.y+height);
// scrolling to bottom without default animation
-(void)scrollToBottom
NSInteger section = [self.clViewImages numberOfSections] - 1;
NSInteger items = [self.clViewImages numberOfItemsInSection:section] - 1;
NSIndexPath *indxPath = [NSIndexPath indexPathForItem:items inSection:section];
[self.clViewImages scrollToItemAtIndexPath:indxPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
-(void)reloadCollectionView
dispatch_async(dispatch_get_main_queue(), ^
[self.clViewImages reloadData];
if(!animationStarted)
[self scrollSlowlyWithInterval:[NSNumber numberWithDouble:0.50f]];
);
感谢Chris
【讨论】:
以上是关于如何使用流畅的动画垂直地为集合视图设置动画的主要内容,如果未能解决你的问题,请参考以下文章