Ios 修改UIScrollView以使用户具有更强的分页保留
我需要一个scrollview,它的行为与UIKit稍有不同 我想获得的是分页的scrollview,但我想使用一些自定义值来表示用户触发分页所需的滚动量 看起来,当用户滚动超过当前页面的50%时,system UIScrollView会触发页面更改。我想让用户更难切换页面Ios 修改UIScrollView以使用户具有更强的分页保留,ios,uiscrollview,Ios,Uiscrollview,我需要一个scrollview,它的行为与UIKit稍有不同 我想获得的是分页的scrollview,但我想使用一些自定义值来表示用户触发分页所需的滚动量 看起来,当用户滚动超过当前页面的50%时,system UIScrollView会触发页面更改。我想让用户更难切换页面 此外,系统UIScrollView还考虑了手势的速度,因此,如果用户滚动很少的像素,但使用非常快的手势,页面就会切换,这是我想要避免的。好的,我用这种方式实现了它,效果非常好: //this is the percentag
此外,系统UIScrollView还考虑了手势的速度,因此,如果用户滚动很少的像素,但使用非常快的手势,页面就会切换,这是我想要避免的。好的,我用这种方式实现了它,效果非常好:
//this is the percentage of page I need to drag to trigger the paging
#define PAGING_THREESHOLD 0.6
//this enum is to keep track of the scrolling direction
typedef enum ScrollDirection {
ScrollDirectionNone,
ScrollDirectionRight,
ScrollDirectionLeft,
ScrollDirectionUp,
ScrollDirectionDown,
ScrollDirectionCrazy,
} ScrollDirection;
//this is to avoid the intertial scrolling
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset {
*targetContentOffset = scrollView.contentOffset;
}
//here is the custom paging code
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
CGPoint destinationOffset = scrollView.contentOffset;
CGFloat y = (int)destinationOffset.y % (int)scrollView.height;
if (_scrollDirection == ScrollDirectionDown){
CGFloat delta = scrollView.height - (scrollView.height - y);
delta = delta / scrollView.height;
NSUInteger currentPage = scrollView.contentOffset.y / scrollView.size.height;
if (delta > PAGING_THREESHOLD && currentPage != scrollView.verticalPageCount - 1){
//go to next page
destinationOffset = CGPointMake(destinationOffset.x, (currentPage + 1) * scrollView.height);
}else{
destinationOffset = CGPointMake(destinationOffset.x, currentPage * scrollView.height);
}
}else if (_scrollDirection == ScrollDirectionUp){
CGFloat delta = scrollView.height - y;
delta = delta / scrollView.height;
NSUInteger currentPage = scrollView.contentOffset.y / scrollView.size.height;
if (delta > PAGING_THREESHOLD){
//go to next page
destinationOffset = CGPointMake(destinationOffset.x, currentPage * scrollView.height);
}else{
destinationOffset = CGPointMake(destinationOffset.x, (currentPage + 1) * scrollView.height);
}
}
if (!CGPointEqualToPoint(destinationOffset, scrollView.contentOffset)){
[scrollView setContentOffset:destinationOffset animated:YES];
}
}
//here the scrolldirection is calculated
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.lastContentOffset.x > scrollView.contentOffset.x){
_scrollDirection = ScrollDirectionRight;
} else if (self.lastContentOffset.x < scrollView.contentOffset.x){
_scrollDirection = ScrollDirectionLeft;
} else if (self.lastContentOffset.y > scrollView.contentOffset.y){
_scrollDirection = ScrollDirectionUp;
} else if (self.lastContentOffset.y < scrollView.contentOffset.y){
_scrollDirection = ScrollDirectionDown;
} else{
_scrollDirection = ScrollDirectionNone;
}
self.lastContentOffset = scrollView.contentOffset;
}
您是否尝试过实施此功能?我相信这可以通过使用无页面的表视图来完成,并查看scrollViewDidScroll中的内容偏移量?我可以通过观看相同的委托方法对无页面滚动视图使用相同的方法吗?对不起,这是一个输入错误。我的意思是滚动视图。实际上,你可能想看看ScrollViewDiEndDraging:WillDecreate:中的偏移量,然后在那里决定是继续下一页还是返回。它成功了,我将在答案中发布代码。谢谢你的提示。