Endless scrolling - setContentOffset: stops slowdown of UIScrollView

I am creating an iPhone app with a large 360 โ€‹โ€‹degree panoramic image. Panorama is a CATiledLayer in a UIScrollView.

I am trying to implement infinite scrolling of an image (horizontal only). I did this by subclassing UIScrollView and implementing setContentOffset: and setContentOffset: animated: and this works great when the user drags the scrollview. However, when the user lifts his finger and the scroll mode slows down, changing the contentOffset parameter causes braking to stop.

- (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 a way to change the contentOffset without affecting the slowdown?

It has been suggested here that overriding setContentOffset: (not setContentOffset: animated) fixes this problem, but I can't get it to work.

I 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); } } 
+6
source share
4 answers

I solved the problem with a workaround. I created a panoramic image with 3 full panorama widths (it does not affect performance too much because I use CATiledLayer ) and set the decelerationRate property to UIScrollViewDecelerationFast . Thus, the user cannot scroll too far to stop braking, and if the deceleration stops in either the left or right panoramic image, the content shift then returns back to the middle image. This has the form of an infinite scroll, and this is the best solution I could come up with.

+2
source

Instead

 [scrollView setContentOffset:tempContentOffset]; 

using

 scrollView.contentOffset = tempContentOffset; 
+1
source

I would try using the protocol method of UIScrollViewDelegate:

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

it calls when the user scrolls (even when it slows down)

and inside it I would change the contentoffset

0
source

I recently did the same endless scroll and accidentally found a solution:

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

What is it, it works for me. :)

0
source

Source: https://habr.com/ru/post/914627/


All Articles