4

I have horizontal UIScrollView with isPagingEnabled = true containing few pages. If I drag scrollview content and release finger it scrolls to next page only if I dragged at least 50% of scrollview width. I want to auto scroll to next/previous page if drag distance is more than 25%.

I was able to achieve it by overriding scrollViewDidEndDragging in UIScrollViewDelegate, calculating drag distance and calling scrollView.setContentOffset. But issue is that if distance > 25 and < 50 then it scrolls back automatically probably because scrollview calls it's default implementation.

Any idea how can I achieve this? Thanks

Martin Vandzura
  • 3,047
  • 1
  • 31
  • 63

1 Answers1

2

I would use the following delegate's callback by modifying offset to desired page

// called on finger up if the user dragged. velocity is in points/millisecond. 
// targetContentOffset may be changed to adjust where the scroll view comes to rest
@available(iOS 5.0, *)
optional func scrollViewWillEndDragging(_ scrollView: UIScrollView, 
    withVelocity velocity: CGPoint, 
    targetContentOffset: UnsafeMutablePointer<CGPoint>)
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • It doesn't work. I don't want different targetContentOffset. I want to animate page to same position. – Martin Vandzura Jan 13 '20 at 08:30
  • Let's align... If page width, say, equal 100 then if being on first page one drags on 26% then the targetContentOffset will be 26 and scrollView returns it back to 0, but if in this case to set targetContentOffset as 100 then it scrolls forward to 2nd page. Isn't it what is expected? – Asperi Jan 13 '20 at 09:04