博客> UISrollView实现pagingEnabled功能,自定义翻页宽度
UISrollView实现pagingEnabled功能,自定义翻页宽度
2017-11-22 05:31 评论:0 阅读:187 深山野猪
ios

const CGFloat pageWidth = 200;

@property (nonatomic, assign) CGFloat direction;


  • (void)scrollViewWillEndDragging:(UIScrollView )scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint )targetContentOffset { self.direction = velocity.x; }

  • (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { [self endScroll:scrollView]; }

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { self.direction = 0; [self endScroll:scrollView]; } }
  • (void)endScroll:(UIScrollView )scrollView { if (scrollView.contentOffset.x > 0) { NSInteger index = scrollView.contentOffset.x/pageWidth; if (self.direction > 0) { index++; } index = MIN(scrollView.contentSize.width/pageWidth, index); [scrollView setContentOffset:CGPointMake(pageWidth index, 0) animated:YES]; } else { [scrollView setContentOffset:CGPointZero animated:YES]; } }
收藏
0
sina weixin mail 回到顶部