45

paging in UIScrollView is a great feature, what I need here is to set the paging to a smaller distance, for example I want my UIScrollView to page less size that the UIScrollView frame width. Thanks

Shardul
  • 4,266
  • 3
  • 32
  • 50
user784625
  • 1,928
  • 5
  • 24
  • 38

11 Answers11

75

There is a UIScrollView delegate method you can use. Set your class as the scroll view's delegate, and then implement the following:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    CGFloat kMaxIndex = 23;
    CGFloat targetX = scrollView.contentOffset.x + velocity.x * 60.0;
    CGFloat targetIndex = 0.0;
    if (velocity.x > 0) {
        targetIndex = ceil(targetX / (kCellWidth + kCellSpacing));
    } else if (velocity.x == 0) {
        targetIndex = round(targetX / (kCellWidth + kCellSpacing));
    } else if (velocity.x < 0) {
        targetIndex = floor(targetX / (kCellWidth + kCellSpacing));
    }
    if (targetIndex < 0)
        targetIndex = 0;
    if (targetIndex > kMaxIndex)
        targetIndex = kMaxIndex;
    targetContentOffset->x = targetIndex * (kCellWidth + kCellSpacing);
    //scrollView.decelerationRate = UIScrollViewDecelerationRateFast;//uncomment this for faster paging
}

The velocity parameter is necessary to make sure the scrolling feels natural and doesn't end abruptly when a touch ends with your finger still moving. The cell width and cell spacing are the page width and spacing between pages in your view. In this case, I'm using a UICollectionView.

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
lucius
  • 8,665
  • 3
  • 34
  • 41
  • 17
    If you want to avoid the scrollview jumping to its initial position when making very small swipes, you may use this : `if (velocity.x > 0) { targetIndex = ceil(targetX / (self.cardViewWidth + kCardSpacing)); } else { targetIndex = floor(targetX / (self.cardViewWidth + kCardSpacing)); }` – Kevin Hirsch Sep 22 '14 at 14:32
  • hi, @lucius! Your code seems to work but I have 1 problem! My pageSize = 340. TargetContentOffset.x calculates correctly, but my scrollView stops at the wrong position so I see margin between views and view (or cell, doesn't matter) (but it has to stop in right position). Maybe u can tell something about it? :) Thanks in advance! – pash3r Oct 07 '14 at 20:29
  • 5
    @lucious, I've figured it out: I had to set scrollView.pagingEnabled to NO :) – pash3r Oct 08 '14 at 11:36
  • 13
    Why targetX expression is multiplied by 60 at the end? This is the only part that i cannot understand. From where does this magic number comes? – KoCMoHaBTa Mar 21 '15 at 21:16
  • 3
    @KoCMoHaBTa "velocity is in points/millisecond" as per the documentation, so this takes into account the force of the drag, by adding the distance that the scroll view would travel in the next 60ms. More powerful drags will scroll further. – Морт Nov 17 '16 at 18:21
  • @lucius But how do we know that the deceleration animation will run for exactly 60ms? – Matthew Quiros Jan 13 '17 at 08:06
  • From trial and error I found that a constant of 900 instead of 60 avoids a glitch when you scroll a small distance very fast. – Robert Oct 24 '17 at 17:11
  • 7
    Don't forget to have a play with `scrollView.decelerationRate` to get the feel you are looking for - I had to set it to `UIScrollViewDecelerationRateFast` before it felt natural – Max Chuquimia Jan 10 '18 at 06:39
  • It will be more helpful, if you give swift implementation also. Because i am not able to assign value to "targetContentOffset" – krishan kumar Jun 08 '19 at 14:37
  • It seems the suggestion by @KevinHirsch is not just a preference but an actual fix to a bug in the behavior of the script by Lucious, I'm therefor editing it in to the answer. While I'm at it I added optional (commented out) high value snippets from the comments. – Albert Renshaw Jul 23 '19 at 03:25
  • Additionally, Kevin's solution has a small issue in which if you page to the right, then over half way through the paging animation, if you 'tap' the scrollview w/o swiping at all, it will launch you back to the previous page. I've fixed this by adding a conditional to use `round` if no velocity.x was found (i.e. tap not swipe) – Albert Renshaw Jul 23 '19 at 05:06
  • @AlbertRenshaw What if the cell has dynamic height? How do i get the height of each UITableViewcell from my UItableview in scrollViewWillEndDragging so i can use a dynamic kCellHeight? I need this because not all cells have the same height in my tableview...any help appreciated. – stefanosn Jun 01 '22 at 20:42
22
  1. Change your scrollView size to the page size you want
  2. Set your scroll.clipsToBounds = NO
  3. Create a UIView subclass (e.g HackClipView) and override the hitTest:withEvent: method

    -(UIView *) hitTest:(CGPoint) point withEvent:(UIEvent *)event
    {     
        UIView* child = [super hitTest:point withEvent:event]; 
        if (child == self && self.subviews.count > 0)  
        {
            return self.subviews[0];
        }
        return child;
    }
    
  4. Set the HackClipView.clipsToBounds = YES

  5. Put your scrollView in this HackClipView (with the total scrolling size you want)

See this answer for more details

Update: As stated in lucius answer you can now implement the UIScollViewDelegate protocol and use the - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset method. As the targetContentOffset is a pointer. Using this method will not guarantee you the same result with scroll view pages as the user can scroll through many pages at once. But setting the descelerationRate to fast will almost give you the same result

Community
  • 1
  • 1
Francescu
  • 16,974
  • 6
  • 49
  • 60
  • 4
    This is a bad answer. If you make the scrollview smaller then the touchable area is also shrunk to the frame you set. The question specifically asks how to handle "a page less than the UIScrollView frame width". – Joshua Jul 24 '13 at 04:04
  • 3
    @Joshua, no, by overriding the hitTest method on the superview you can redirect the touch on the scrollView child. – Francescu Jul 24 '13 at 14:00
  • @Francescu @Joshua or override `pointInside:withEvent:` in a scroll view subclass. – Frank Schmitt Jul 11 '14 at 22:15
  • 1
    Won't work with UITableView & UICollectionView, cause the cells out of the original bounds (normally "hidden" but visible thanks to `clipToBounds=NO`) will be removed from superview. – Martin Oct 28 '15 at 11:05
  • Tried with a UICollectionView and the cells from outside the visible area are hidden/reused, except the next one in the direction of scrolling. Did not succeed to trick it to cache more cells. Also, drag touch works only if it starts inside the small scrollView – codrut Jul 11 '18 at 14:21
11

You should disable paging and add a UIPanGestureRecognizer to your scroll view and handle the paging yourself.

- (void)viewDidLoad {

    [super viewDidLoad];
    CGRect viewRect = self.view.bounds; // View controller's view bounds
    theScrollView = [[UIScrollView alloc] initWithFrame:viewRect]; 

    theScrollView.scrollsToTop      = NO;
    theScrollView.pagingEnabled         = NO;
    theScrollView.delaysContentTouches  = NO;
    theScrollView.delegate = self;

    [self.view addSubview:theScrollView];

    UIPanGestureRecognizer * peter = [[[UIPanGestureRecognizer alloc] initWithTarget:self  
                                                                              action:@selector(handlePan:)]
                                       autorelease]; 
    [theScrollView addGestureRecognizer:peter]; 

}

-(void)handlePan:(UIPanGestureRecognizer*)recognizer{

    switch (recognizer.state) {
    case UIGestureRecognizerStateBegan:{
        // panStart and startPoint are instance vars for the viewContainer 
        panStart = theScrollView.contentOffset;
        startPoint = [recognizer locationInView:theScrollView]; 
        
        
        break;
    }
    case UIGestureRecognizerStateChanged:{
                    
        CGPoint newPoint = [recognizer locationInView:theScrollView];
        CGFloat delta = startPoint.x - newPoint.x;
        if ( abs(delta) > 2)
            theScrollView.contentOffset = CGPointMake( theScrollView.contentOffset.x + delta, 0); 
        
        CGFloat moveDelta = panStart.x - theScrollView.contentOffset.x;                               
        

        // current witdh should hold the currently displayed page/view in theScrollView
        if ( abs(moveDelta) > (currentWidth * 0.40)){
            panStart = theScrollView.contentOffset;
            startPoint = newPoint;
            
            //NSLog(@"delta is bigger"); 
            if ( moveDelta < 0 )
                [self incrementPageNumber]; // you should implement this method and present the next view
            else 
                [self decrementPageNumber]; // you should implement this method and present the previous view
   
            recognizer.enabled = NO; // disable further event until view change finish
                 
        }
        
        break; 
    }
        
    case UIGestureRecognizerStateEnded:
    case UIGestureRecognizerStateCancelled:

        recognizer.enabled = YES; 
        [self showDocumentPage:currentPage]; 
        
        break;
        
        
    default:
        break;
    }
}
beryllium
  • 29,669
  • 15
  • 106
  • 125
aimless
  • 605
  • 6
  • 13
6

Swift 4.1 solution that simplifies reusing:

/// Protocol that simplifies custom page size configuration for UIScrollView.
/// Sadly, can not be done better due to protocol extensions limitations - https://stackoverflow.com/questions/39487168/non-objc-method-does-not-satisfy-optional-requirement-of-objc-protocol
/// - note: Set `.decelerationRate` to `UIScrollViewDecelerationRateFast` for a fancy scrolling animation.
protocol ScrollViewCustomHorizontalPageSize: UIScrollViewDelegate {
    /// Custom page size
    var pageSize: CGFloat { get }

    /// Helper method to get current page fraction
    func getCurrentPage(scrollView: UIScrollView) -> CGFloat

    /// Helper method to get targetContentOffset. Usage:
    ///
    ///     func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    ///         targetContentOffset.pointee.x = getTargetContentOffset(scrollView: scrollView, velocity: velocity)
    ///     }
    func getTargetContentOffset(scrollView: UIScrollView, velocity: CGPoint) -> CGFloat

    /// Must be implemented. See `getTargetContentOffset` for more info.
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
}

extension ScrollViewCustomHorizontalPageSize {
    func getCurrentPage(scrollView: UIScrollView) -> CGFloat {
        return (scrollView.contentOffset.x + scrollView.contentInset.left) / pageSize
    }

    func getTargetContentOffset(scrollView: UIScrollView, velocity: CGPoint) -> CGFloat {
        let targetX: CGFloat = scrollView.contentOffset.x + velocity.x * 60.0

        var targetIndex = (targetX + scrollView.contentInset.left) / pageSize
        let maxOffsetX = scrollView.contentSize.width - scrollView.bounds.width + scrollView.contentInset.right
        let maxIndex = (maxOffsetX + scrollView.contentInset.left) / pageSize
        if velocity.x > 0 {
            targetIndex = ceil(targetIndex)
        } else if velocity.x < 0 {
            targetIndex = floor(targetIndex)
        } else {
            let (maxFloorIndex, lastInterval) = modf(maxIndex)
            if targetIndex > maxFloorIndex {
                if targetIndex >= lastInterval / 2 + maxFloorIndex {
                    targetIndex = maxIndex
                } else {
                    targetIndex = maxFloorIndex
                }
            } else {
                targetIndex = round(targetIndex)
            }
        }

        if targetIndex < 0 {
            targetIndex = 0
        }

        var offsetX = targetIndex * pageSize - scrollView.contentInset.left
        offsetX = min(offsetX, maxOffsetX)

        return offsetX
    }
}

Just conform to ScrollViewCustomPageSize protocol in your UIScrollView/UITableView/UICollectionView delegate and you are done, e.g.:

extension MyCollectionViewController: ScrollViewCustomPageSize {
    var pageSize: CGFloat {
        return 200
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        targetContentOffset.pointee.x = getTargetContentOffset(scrollView: scrollView, velocity: velocity)
    }
}

For a fancy scrolling I also recommend to set collectionView.decelerationRate = UIScrollViewDecelerationRateFast

Anton Plebanovich
  • 1,296
  • 17
  • 17
  • is there a way to lock scroll to only one page per action? using following approach I sometimes can scroll though two pages, but want only one in each direction. sometimes now: 0 - 1 - 3 - 4, want : 0 - 1 - 2 - 3 - 4 – mkz Jun 01 '19 at 21:30
  • @mkz hi, you can just clamp `targetIndex` before calculating `offsetX` in `getTargetContentOffset` method. I didn't test but something like that should work: `targetIndex = max(targetIndex, floor(getCurrentPage(scrollView: scrollView)); targetIndex = min(targetIndex, ceil(getCurrentPage(scrollView: scrollView))` – Anton Plebanovich Jun 03 '19 at 10:20
  • The methods are triggered but there's no limit in the width. Do you have an example? – bruno Nov 06 '20 at 15:53
  • 1
    @bruno you may check https://github.com/APUtils/APExtensions/tree/3b238b06118413ed20bfd4ed6e661200822c3fa9 there is a custom page size example – Anton Plebanovich Nov 06 '20 at 19:12
1

I had the same problem so I have made a custom UIScrollView. It's available on Github now because when I searched I didn't find any solutions like this. Enjoy! https://github.com/MartinMetselaar/MMCPSScrollView

MMCPSScrollView* scrollView = [[MMCPSScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setType:MMCPSScrollVertical];
[scrollView setPageHeight:250];
[scrollView setPageSize:2];
[self.view addSubview:scrollView];

If you have any further questions about this component, just ask.

Martin Metselaar
  • 347
  • 1
  • 2
  • 11
1

Adding gesture recognizers or other subviews and so on is silly. Just set the delegate for the scroll view an imlement on of the below :

// This is for a vertical scrolling scroll view. 
// Let's say you want it to snap to every 160 pixels :    

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{     
     int y = scrollView.contentOffset.y;
     int yOff = y % 160;
     if(yOff < 80)
          y -= yOff;
     else
          y += 160 - yOff;

     [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, y) animated:YES];
}

// This is for a horizontal scrolling scroll view.
// Let's say you want the same, to snap to every 160 pixels :

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
     int x = scrollView.contentOffset.x;
     int xOff = x % 160;
     if(xOff < 80)
          x -= xOff;
     else
          x += 160 - xOff;

     [scrollView setContentOffset:CGPointMake(x, scrollView.contentOffset.y) animated:YES];
}
Dani Pralea
  • 4,545
  • 2
  • 31
  • 49
1

Swift 4.1, iOS11+:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    targetContentOffset.pointee = CGPoint(
        x: round(targetContentOffset.pointee.x / pageWidth) * pageWidth,
        y: targetContentOffset.pointee.y
    )
}
Deniss Fedotovs
  • 1,384
  • 12
  • 22
1

Set the contentOffset in -(void)scrollViewDidScroll:(UIScrollView *)scrollView method.

Also refer to UIScrollViewDelegate refernces

Shrey
  • 1,959
  • 2
  • 21
  • 44
0

This seemed to work a lot better for me:

UIScrollView Custom Paging

Here they are adding the scrollview (keeping it's paging niceness) as a subview to an ExtendedTouchView or subclass of UIVIew and overwriting the hit test method

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
    if ([[self subviews] count] > 0) {
        //force return of first child, if exists
        return [[self subviews] objectAtIndex:0];
    } else {
        return self;
    }
}
return nil;
}

This did exactly whatI wanted with minimal code and headache.

Community
  • 1
  • 1
brad.roush
  • 597
  • 1
  • 4
  • 9
0

I had the same problem short ago. My aproach was to add a second UIScrollView to the scrollview. So you can switch to the page. On that page it seems than if the page is bigger than the screen. I hope it works also in your situation. ;-)

Sandro Meier

Sandro Meier
  • 3,071
  • 2
  • 32
  • 47
-2

The easiest way is to add this code

scrollView.clipsToBounds = false
scrollView.removeGestureRecognizer(scrollView.panGestureRecognizer)
view.addGestureRecognizer(scrollView.panGestureRecognizer)
lennartk
  • 570
  • 1
  • 4
  • 15