iOS - 实现scrollView和collectionView自定义分页

//主要代码

  • (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    CGFloat x = targetContentOffset->x;
    CGFloat pageWidth = itemWidth + 20;
    CGFloat movedX = x - pageWidth * selectedIndex;
    if (movedX < -pageWidth * 0.5) {
    // Move left
    selectedIndex--;
    } else if (movedX > pageWidth * 0.5) {
    // Move right
    selectedIndex++;
    }

    if (ABS(velocity.x) >= 2){
    targetContentOffset->x = pageWidth * selectedIndex;
    } else {
    targetContentOffset->x = scrollView.contentOffset.x;
    [scrollView setContentOffset:CGPointMake(pageWidth * selectedIndex, scrollView.contentOffset.y) animated:YES];
    }
    NSLog(@"%ld",selectedIndex);
    }

你可能感兴趣的:(iOS - 实现scrollView和collectionView自定义分页)