UIScrollView 使其平滑的滚动到指定位置

UIScrollView 使其平滑的滚动到指定位置

使用UISCrollView的代理方法:

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

targetContentOffset是scrollView将要到达的偏移量,只需在代理方法下修改targetContentOffset的值,即可使scrollVIew平滑的滚动到指定的位置;


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

 {

CGPoint targetOffset = [self nearestTargetOffsetForOffset:*targetContentOffset]; 

 targetContentOffset->x = targetOffset.x; 

 targetContentOffset->y = targetOffset.y; 

}


- (CGPoint)nearestTargetOffsetForOffset:(CGPoint)offset

 {

CGFloat pageSize = BUBBLE_DIAMETER + BUBBLE_PADDING; 

NSInteger page = roundf(offset.x / pageSize);

CGFloat targetX = pageSize * page; 

return CGPointMake(targetX, offset.y); 

}

需要注意的是,此种方法,只有在scrollView的pagingEnable属性值为NO时才被调用;

推荐文章:http://tech.glowing.com/cn/practice-in-uiscrollview/

你可能感兴趣的:(iOS)