UITableView with custom paging-like behaviour

前端 未结 3 1844
一整个雨季
一整个雨季 2020-12-28 10:53

I am trying to implement paging for custom sized pages in the UITableView. What I am trying to achieve is to have the top of the active cell align with the top of the tabl

3条回答
  •  孤独总比滥情好
    2020-12-28 11:43

    I solved this problem with this code:

    - (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
        CGFloat pageWidth = self.collectionView.frame.size.width + 10 /* Optional Photo app like gap between images */;
    
        _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;
    
        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);
    }
    

    Thanks to http://www.mysamplecode.com/2012/12/ios-scrollview-example-with-paging.html for pointing the right way.

提交回复
热议问题