Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deactivate UIScrollView decelerating

Is there a way to deactivate the decelerating of a UIScrollView?

I want to allow the user to scroll the canvas, but I don't want that the canvas continues scrolling after the user lifted the finger.

like image 581
Markus Müller-Simhofer Avatar asked Jul 09 '09 18:07

Markus Müller-Simhofer


People also ask

How do I stop UIScrollView scrolling?

For disabling the same we need to make the “isScrollEnabled” property of our scroll view to false. Copy the below code in your file. import UIKit class ViewController: UIViewController { @IBOutlet var scrollView: UIScrollView! override func viewDidLoad() { super.

What is ScrollView in Swift?

The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.


2 Answers

This can be done by utilizing the UIScrollView delegate method scrollViewWillBeginDecelerating to automatically set the content offset to the current screen position.

To implement:

  1. Assign a delegate to your UIScrollView object if you have not already done so.
  2. In your delegate's .m implementation file, add the following lines of code:

    -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{       [scrollView setContentOffset:scrollView.contentOffset animated:YES];    } 

Voila! No more auto-scroll.

like image 54
Mark Avatar answered Sep 23 '22 08:09

Mark


For iOS 5.0 or later, there is a better method than calling setContentOffset:animated:.

Implement delegate method scrollViewWillEndDragging:withVelocity:targetContentOffset: in your .m file:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView                      withVelocity:(CGPoint)velocity               targetContentOffset:(inout CGPoint *)targetContentOffset {     targetContentOffset.pointee = scrollView.contentOffset; } 

Assigning the current offset to targetContentOffset stops the UIScrollView from auto-scrolling.

like image 26
Quotation Avatar answered Sep 22 '22 08:09

Quotation