UIScrollView custom paging size

后端 未结 11 1551
走了就别回头了
走了就别回头了 2020-12-07 11:49

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 UIScrol

11条回答
  •  悲&欢浪女
    2020-12-07 12:06

    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;
        }
    }
    

提交回复
热议问题