6

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);
    }
}
4

4 回答 4

4

代替

[scrollView setContentOffset:tempContentOffset];

利用

scrollView.contentOffset = tempContentOffset;
于 2016-03-31T19:23:11.013 回答
2

我通过解决方法解决了这个问题。我创建了一个具有 3 个全宽全景图的全景图(不会对性能产生太大影响,因为我使用的是CATiledLayer),并将decelerationRate属性设置为UIScrollViewDecelerationFast. 因此,在减速停止之前,用户无法滚动太多,如果在左侧或右侧全景图像中减速停止,则内容偏移量将变回中间图像。这具有无限滚动的外观,这是我能想到的最佳解决方案。

于 2012-07-13T02:01:20.090 回答
1

我最近在做同样的无限滚动,无意中找到了解决方案:

只需设置bounces=YES, alwaysBounceHorizontal=YESor/and alwaysBounceVertical=YES(取决于您滚动到的方向)。

就是这样,这对我有用。:)

于 2014-01-19T16:57:20.317 回答
0

我会尝试使用 UIScrollViewDelegate 协议方法:

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

当用户滚动时调用它(即使它正在减速)

在里面我会改变 contentoffset

于 2012-05-01T18:59:31.997 回答