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);
}
}
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 thedecelerationRateproperty toUIScrollViewDecelerationFast. 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.