Uiscrollview Custom Paging

UIScrollView Custom Paging

I just did this for another project. What you need to do is to place the UIScrollView into a custom implementation of UIView. I created a class for this called ExtendedHitAreaViewController. The ExtendedHitAreaView overrides the hitTest function to return its first child object, which will be your scroll view.

Your scroll view should be the page size you want, i.e., 30px with clipsToBounds = NO.
The extended hit area view should be the full size of the area you want to be visible, with clipsToBounds = YES.

Add the scroll view as a subview to the extended hit area view, then add the extended hit area view to your viewcontroller's view.

@implementation ExtendedHitAreaViewContainer

- (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;
}
@end

UIScrollView paging from center to left and right

You should make 3 pages, so the contentSize should be 3 times the size of one page, as far as i can see, that is happening.

Now make sure the user always starts on page 2 (the center page), do this by setting the contentOffset to 1x the width of the scrollView for the x coordinate and 0 for the y coordinate.

After that, the user should be able to scroll left and right.

PS: by looking at your code, it seems like you are assuming everybody uses an iPhone 6 (width of 375pt), you might want to use the calculated width (by using self.view.frame.size.width or something relative)

iOS : How to do proper paging in UIScrollView?

Paging scroll views alway page multiples of their frame size. So in your example paging is always +320.

This behavior is good if you have content portions matching the frame of the scroll view.

What you have to do, is giving your scroll view a width of 213 and set its clipsToBounds property to NO.
After that your scroll view pages exactly how you want and you see what's left and right outside the frame.
Additionally you have to do a trick to make this left and right area delegate touches to the scroll view.

It's greatly explained in this answer.

Custom UIScrollView paging with scrollViewWillEndDragging

You can implement custom paging with this code:

- (float) pageWidth {
return ((UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout).itemSize.width +
((UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout).minimumInteritemSpacing;
}

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {

CGFloat pageWidth = self.collectionView.frame.size.width + 10 /* Optional Photo app like gap between images. Or use [self pageWidth] in case if you want the next page be also visible */;

_currentPage = floor((self.collectionView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

NSLog(@"Dragging - You are now on page %i", _currentPage);
}

- (void) scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset {

CGFloat pageWidth = self.collectionView.frame.size.width + 10; // [self pageWidth]

int newPage = _currentPage;

if (velocity.x == 0) { // slow dragging not lifting finger
newPage = floor((targetContentOffset->x - pageWidth / 2) / pageWidth) + 1;
}
else {
newPage = velocity.x > 0 ? _currentPage + 1 : _currentPage - 1;

if (newPage < 0)
newPage = 0;
if (newPage > self.collectionView.contentSize.width / pageWidth)
newPage = ceil(self.collectionView.contentSize.width / pageWidth) - 1.0;
}

NSLog(@"Dragging - You will be on %i page (from page %i)", newPage, _currentPage);

*targetContentOffset = CGPointMake(newPage * pageWidth, targetContentOffset->y);
}

Of course you must set pagingEnabled = NO.
_currentPage is a class iVar.
Thanks to http://www.mysamplecode.com/2012/12/ios-scrollview-example-with-paging.html for pointing the right way.

Paging UIScrollView in increments smaller than frame size

Try making your scrollview less than the size of the screen (width-wise), but uncheck the "Clip Subviews" checkbox in IB. Then, overlay a transparent, userInteractionEnabled = NO view on top of it (at full width), which overrides hitTest:withEvent: to return your scroll view. That should give you what you're looking for. See this answer for more details.



Related Topics



Leave a reply



Submit