Sensitivity/Scroll speed of UIScrollView with paging
This is fairly straightforward, however you must implement the paging aspect yourself (which is fairly simple). You don't need gesture recognizers.
Swift
First, adjust your UIScrollView deceleration rate:
scrollView.decelerationRate = UIScrollViewDecelerationRateFast
Assume we have an array of content — let's call it yourPagesArray
. In your UIScrollViewDelegate, implement the following method:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
{
//This is the index of the "page" that we will be landing at
let nearestIndex = Int(CGFloat(targetContentOffset.pointee.x) / scrollView.bounds.size.width + 0.5)
//Just to make sure we don't scroll past your content
let clampedIndex = max( min( nearestIndex, yourPagesArray.count - 1 ), 0 )
//This is the actual x position in the scroll view
var xOffset = CGFloat(clampedIndex) * scrollView.bounds.size.width
//I've found that scroll views will "stick" unless this is done
xOffset = xOffset == 0.0 ? 1.0 : xOffset
//Tell the scroll view to land on our page
targetContentOffset.pointee.x = xOffset
}
You will also need to implement the following delegate method, to handle the case where the user lifts their finger gently without causing any scroll deceleration:
(Update: you may not need to do this under the latest iOS SDK. It seems like the above delegate method is now called when there is zero velocity.)
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
if !decelerate
{
let currentIndex = floor(scrollView.contentOffset.x / scrollView.bounds.size.width)
let offset = CGPoint(x: scrollView.bounds.size.width * currentIndex, y: 0)
scrollView.setContentOffset(offset, animated: true)
}
}
Objective-C
Setting your deceleration rate:
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
Then your scroll view delegate implementation:
- (void) scrollViewWillEndDragging:(UIScrollView *)scroll withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
//This is the index of the "page" that we will be landing at
NSUInteger nearestIndex = (NSUInteger)(targetContentOffset->x / scrollView.bounds.size.width + 0.5f);
//Just to make sure we don't scroll past your content
nearestIndex = MAX( MIN( nearestIndex, yourPagesArray.count - 1 ), 0 );
//This is the actual x position in the scroll view
CGFloat xOffset = nearestIndex * scrollView.bounds.size.width;
//I've found that scroll views will "stick" unless this is done
xOffset = xOffset==0?1:xOffset;
//Tell the scroll view to land on our page
*targetContentOffset = CGPointMake(xOffset, targetContentOffset->y);
}
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if( !decelerate )
{
NSUInteger currentIndex = (NSUInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);
[scrollView setContentOffset:CGPointMake(scrollView.bounds.size.width * currentIndex, 0) animated:YES];
}
}
Adjusting UICollectionView scroll speed/sensitivity
UICollectionView
is a subclass of UIScrollView
, so you can adjust the decelerationRate
in your collection view controller's viewDidLoad
, like so:
Objective-C:
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
};
Swift:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast
}
Swift -prevent fast scrolling when collectionView.isPagingEnabled = true
this answer worked:
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
scrollView.isUserInteractionEnabled = false
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollView.isUserInteractionEnabled = true
}
Related Topics
A Launch Storyboard or Xib Must Be Provided Unless the App Requires Full Screen
How to Add Text Input in Alertview of iOS 8
How to Convert .Dae to .Scn Files in Scenekit
Which Tasks Are More Suitable to Nsoperation Than Gcd
How to Make Sure API Requests Come from Our Mobile (Ios/Android) App
Profile Doesn't Match the Entitlements File's Value for the Application-Identifier Entitlement
App Crashes Only on Testflight Build
Swift - iOS - Dates and Times in Different Format
How to Loop Over Struct Properties in Swift
No Round Rect Button in Xcode 5
Warning: Attempt to Present Viewcontroller Whose View Is Not in the Window Hierarchy
How to Track User Location in Background