ios – 在UICollectionView中创建缓慢滚动到indexPath

前端之家收集整理的这篇文章主要介绍了ios – 在UICollectionView中创建缓慢滚动到indexPath前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一个项目,我正在使用UICollectionView来创建一个“图像报告器”,我在那里广告一系列标志. collectionView是一个项目高和十二个项目长,一次显示两到三个项目(取决于徽标的大小可见).

我想从第一个项目到最后一个缓慢的自动滚动动画,然后重复.

有没有人能够做这项工作?我可以使滚动工作

[myCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(myImages.count -1) inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];

但这太过分了!

[UIView animateWithDuration:10 delay:2 options:(UIViewAnimationOptionAutoreverse + UIViewAnimationOptionRepeat) animations:^{
    [myCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(myImages.count -1) inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
} completion:nil];

这产生了所需的滚动速度,但是只有最后几个单元格在系列中可见.我怀疑他们(甚至起始的可见细胞)正在出场.

有什么想法吗?

解决方法

你可以尝试这种方法
@property (nonatomic,assign) CGPoint scrollingPoint,endPoint;
@property (nonatomic,strong) NSTimer *scrollingTimer;
@synthesize scrollingPoint,endPoint;
@synthesize scrollingTimer;

- (void)scrollSlowly {
    // Set the point where the scrolling stops.
    self.endPoint = CGPointMake(0,300);
    // 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:0.015 target:self selector:@selector(scrollSlowlyToPoint) userInfo:nil repeats:YES];
}

- (void)scrollSlowlyToPoint {
    self.collectionView.contentOffset = self.scrollingPoint;
    // 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];
    }
    // Going one pixel to the right.
    self.scrollingPoint = CGPointMake(self.scrollingPoint.x,self.scrollingPoint.y+1);
}
原文链接:https://www.f2er.com/iOS/336810.html

猜你在找的iOS相关文章