Infinite Scrolling - setContentOffset: stops decel

2019-03-28 09:56发布

问题:

I am creating an iPhone app with a large, 360 degree, panorama image. The panorama is a CATiledLayer in a UIScrollView.

I am attempting to implement infinite scrolling on the image (horizontal only). I have done this by subclassing UIScrollView and implementing setContentOffset: and setContentOffset:animated: and this works perfectly when the user is dragging the scrollview. However, when the user has lifted their finger and the scrollview is decelerating, changing the contentOffset causes the deceleration to stop instantly.

- (void)setContentOffset:(CGPoint)contentOffset 
{    
    CGPoint tempContentOffset = contentOffset;

    if ((int)tempContentOffset.x >= 5114)
    {
        tempContentOffset = CGPointMake(1, tempContentOffset.y);
    }
    else if ((int)tempContentOffset.x <= 0)
    {
        tempContentOffset = CGPointMake(5113, tempContentOffset.y);
    }

    [super setContentOffset:tempContentOffset];    
}

Is there any way to change the contentOffset without affecting deceleration?

It was suggested here that overriding setContentOffset: (not setContentOffset:animated:) fixes this issue, but I can't seem to get it working.

I have also tried scrollRectToVisible:animated: with no success.

Any ideas on how to fix this problem would be greatly appreciated. Thanks!

EDIT:

Code for scrollViewDidScroll:

-(void)scrollViewDidScroll:(PanoramaScrollView *)scrollView
{
    [panoramaScrollView setContentOffset:panoramaScrollView.contentOffset];
}   

I also tried this:

-(void)scrollViewDidScroll:(PanoramaScrollView *)scrollView
{
    CGPoint tempContentOffset = panoramaScrollView.contentOffset;

    if ((int)tempContentOffset.x >= 5114)
    {
        panoramaScrollView.contentOffset = CGPointMake(1, panoramaScrollView.contentOffset.y);
    }
    else if ((int)tempContentOffset.x == 0)
    {
        panoramaScrollView.contentOffset = CGPointMake(5113, panoramaScrollView.contentOffset.y);
    }
}

回答1:

Instead of

[scrollView setContentOffset:tempContentOffset];

use

scrollView.contentOffset = tempContentOffset;


回答2:

I resolved the issue with a workaround. I created a panorama image with 3 full widths of the panorama (doesnt affect performance too much because I'm using CATiledLayer), and set the decelerationRate property to UIScrollViewDecelerationFast. Therefore, the user is unable to scroll too far before the deceleration stops, and if the deceleration stops in either the left or right panorama image, the content offset is then changed back to the middle image. This has the appearance of infinite scrolling, and it's the best solution I could come up with.



回答3:

i would try to use the UIScrollViewDelegate protocol method:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

it's called when user scroll (even when it's decelerating)

and inside it i would change the contentoffset



回答4:

I recently was doing the same infinite scrolling and accidentally found the solution:

Just set bounces=YES, alwaysBounceHorizontal=YES or/and alwaysBounceVertical=YES (depends on direction you scrolling to).

That's it, this works for me. :)