在UICollectionView中创建到indexPath的慢速滚动

Chr*_*ris 19 scroll nsindexpath uiviewanimation ios uicollectionview

我正在开发一个项目,我正在使用UICollectionView创建一个'图像自动收报机',我正在宣传一系列徽标.collectionView是一个项目高和十二个项目长,并一次显示两到三个项目(取决于可见的徽标的大小).

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

有没有人能够做到这一点?我可以使用滚动工作

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

但这太快了!

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

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

有什么想法吗?

Mas*_*asa 16

你可以尝试这种方法:

@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);
}
Run Code Online (Sandbox Code Playgroud)


Hlu*_*ung 12

我使用"1像素偏移"技巧.以编程方式滚动时,只需将结尾设置为contentOffset.x比它应该更多/更少的1个像素.这应该是不明显的.并在完成块中将其设置为实际值.这样你可以避免过早的单元格出列问题并获得平滑的滚动动画;)

这是滚动到右页的示例(例如,从第1页到第2页).请注意,在animations:块中我实际上滚动了一个像素(pageWidth * nextPage - 1).然后我恢复completion:块中的正确值.

CGFloat pageWidth = self.collectionView.frame.size.width;
int currentPage = self.collectionView.contentOffset.x / pageWidth;
int nextPage = currentPage + 1;

[UIView animateWithDuration:1
                      delay:0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     [self.collectionView setContentOffset:CGPointMake(pageWidth * nextPage - 1, 0)];
                 } completion:^(BOOL finished) {
                     [self.collectionView setContentOffset:CGPointMake(pageWidth * nextPage, 0)];
                 }];
Run Code Online (Sandbox Code Playgroud)

  • 无缘无故的投票无效,你知道吗? (5认同)

Pau*_*len 8

你也可以在你的末尾有一个"慢"滚动,UICollectionView而不必从indexpath"跳转"到indexpath.我快速创建了这个TVOS应用程序的集合视图:

func autoScroll () {
    let co = collectionView.contentOffset.x
    let no = co + 1

    UIView.animateWithDuration(0.001, delay: 0, options: .CurveEaseInOut, animations: { [weak self]() -> Void in
        self?.collectionView.contentOffset = CGPoint(x: no, y: 0)
        }) { [weak self](finished) -> Void in
            self?.autoScroll()
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是打电话给autoScroll(),viewDidLoad()其余的照顾它本身.滚动的速度由动画的时间决定UIView.您可以(我还没有尝试过)添加NSTimer 0而不是秒,这样您就可以在userscroll上使其无效.


All*_*eir 5

对于发现此问题的其他人,我已经更新了 Masa 的建议以在 Swift 上工作,并且我在最后引入了一点缓和,因此它的作用更像是原始的 scrollItemsToIndexPath 动画调用。我有数百个项目在我看来,所以稳定的步伐对我来说不是一个选择。

var scrollPoint: CGPoint?
var endPoint: CGPoint?
var scrollTimer: NSTimer?
var scrollingUp = false

func scrollToIndexPath(path: NSIndexPath) {
    let atts = self.collectionView!.layoutAttributesForItemAtIndexPath(path)
    self.endPoint = CGPointMake(0, atts!.frame.origin.y - self.collectionView!.contentInset.top)
    self.scrollPoint = self.collectionView!.contentOffset
    self.scrollingUp = self.collectionView!.contentOffset.y > self.endPoint!.y

    self.scrollTimer?.invalidate()
    self.scrollTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "scrollTimerTriggered:", userInfo: nil, repeats: true)
}

func scrollTimerTriggered(timer: NSTimer) {
    let dif = fabs(self.scrollPoint!.y - self.endPoint!.y) / 1000.0
    let modifier: CGFloat = self.scrollingUp ? -30 : 30

    self.scrollPoint = CGPointMake(self.scrollPoint!.x, self.scrollPoint!.y + (modifier * dif))
    self.collectionView?.contentOffset = self.scrollPoint!

    if self.scrollingUp && self.collectionView!.contentOffset.y <= self.endPoint!.y {
        self.collectionView!.contentOffset = self.endPoint!
        timer.invalidate()
    } else if !self.scrollingUp && self.collectionView!.contentOffset.y >= self.endPoint!.y {
        self.collectionView!.contentOffset = self.endPoint!
        timer.invalidate()
    }
}
Run Code Online (Sandbox Code Playgroud)

调整 dif 和 modifier 的值可以调整大多数情况下的持续时间/轻松程度。