停止UIScrollView的减速

停止UIScrollView的减速

本文介绍了无限滚动 - setContentOffset:停止UIScrollView的减速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有360度全景图像的iPhone应用程序。全景图是UIScrollView中的CATiledLayer。

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

我试图在图像上实现无限滚动(仅水平)。我通过继承UIScrollView并实现setContentOffset:和setContentOffset:animated来完成此操作:当用户拖动滚动视图时,这非常有效。但是,当用户抬起手指并且滚动视图正在减速时,更改contentOffset会导致减速立即停止。

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];
}

有没有办法在不影响减速的情况下更改contentOffset?

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

建议覆盖setContentOffset :(不是setContentOffset :动画:)修复了这个问题,但我似乎无法让它工作。

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

我也尝试过scrollRectToVisible:动画:没有成功。

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

如何解决这个问题的任何想法将不胜感激。谢谢!

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

编辑:

scrollViewDidScroll的代码:

Code for scrollViewDidScroll:

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

我也试过这个:

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


推荐答案

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

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.

这篇关于无限滚动 - setContentOffset:停止UIScrollView的减速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:39