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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章