Change the scroll speed of a UIScrollView

In my application, I have a view that extends the UIScrollView and populates its contents as the user scrolls. However, if the user scrolls the views filled in inside the UIScrollView too quickly, they are not created in time, and you can really see the background of the UIScrollView. The reason this happens is because I am doing this lazy loading in layoutSubviews does not seem to cause a call every time the contentOffset property is changed.

If you scroll through the UIScrollView slowly, you do not encounter the problem described above, and the content loads quickly enough.

The only solution to this problem that I can think of is to make sure that the UIScrollView does not scroll faster than a certain speed when the user lifts his finger from the screen.

Just so you know that changing the decelerationRate property is not my solution. DecelerationRate tells UIScrollView how quickly it should stop scrolling after the user lifts a finger.

thanks in advance,

Fbr

+3
source share
2 answers

scroll.pagingEnabled = YES;

+1
source

I had the same problem. I solved this with a two-way approach (iOS 8.3, Swift 1.2):

  • Preparation

    • Add the scrollview delegate to your class:

      class myCustomClassName:UIViewController,UIScrollViewDelegate { } 
    • Made my class a scroll delegate:

       self.myScrollView.delegate = self 
    • Added delegate method in which I get contentOffset (scrollable position):

       func scrollViewDidScroll(scrollView: UIScrollView) { //some code... } 
  • Added "content ranges" with bool checks if data has already been added:

     func scrollViewDidScroll(scrollView: UIScrollView) { //Total products = TP if self.useScrollViewDynamicLoading { var scrollPosition:CGFloat = scrollView.contentOffset.y switch scrollPosition { case 200.0...300.0: //TP = 24 if self.checkIfProductsInGivenRangeHaveAlreadyBeenAdded(18) { loadProductsForProductRange(12) } case 600.0...700.0: //TP = 36 if self.checkIfProductsInGivenRangeHaveAlreadyBeenAdded(24) { loadProductsForProductRange(18) } } .... 
  • In viewDidLoad() , the scroll deceleration rate has changed:

     self.myScrollView.decelerationRate = 0.5 
0
source

All Articles