我正在使用10个自定义视图实现水平滚动视图,每个视图都有不同的UI。当用户向左滚动时,我滚动到下一个视图,在右滚动到上一个视图,它工作正常,直到现在。在某些情况下,我必须限制左滚动并只启用右滚动。 在下面的方法中我得到了禁用右滚动的方案
-(void)scrollEnabled:(BOOL)scrollEnable
{
self.scrollView.scrollEnabled = scrollEnable;
// scrollEnable comes NO at some conditions here i have to disable the right scrolling.
if(!scrollEnable)
{
// Now i am registering the pan gesture and detecting the right swiping
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(detectRightScroll:)];
[self.panView addGestureRecognizer:panGesture];
}
}
- (void) detectRightScroll:(UIPanGestureRecognizer *)gesture {
CGPoint translation = [gesture translationInView:gesture.view];
if(translation > 0) // here detecting the right scrolling and changing the scrollview content offset
{
[scrollView setContentOffset: CGPointMake(scrollView.contentOffset.x-translation, 0)];
view is scrolling but not scrolling like scrooview
}
}
我们可以在不注册手势的情况下达到我的要求。 我在过去的4天里遇到了这个问题,但无法解决。 任何帮助都可以得到赞赏。 感谢。
答案 0 :(得分:1)
如果方向错误(或禁用该方向),请尝试重置contentOffset
UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x > stopPosition) { // or (scrollView.contentOffset.x < stopPosition)
[scrollView setContentOffset:CGPointMake(stopPosition, 0)];
}
}
答案 1 :(得分:1)
尝试使用
scrollViewDidEndDecelerating:(UIScrollView )scrollView and - (void)scrollViewDidEndDragging:(UIScrollView )scrollView willDecelerate:(BOOL)decelerate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int xFinal = self.yourScroll.contentOffset.x ;
int viewIndex = xFinal / (yourImageSize);
xFinal = viewIndex * yourImageSize ;
[self.yourScroll setContentOffset:CGPointMake(xFinal, 0) animated:YES];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
int xFinal = self.yourScroll.contentOffset.x ;
int viewIndex = xFinal / yourImageSize ;
xFinal = viewIndex * yourImageSize;
[self.yourScroll setContentOffset:CGPointMake(xFinal, 0) animated:YES];
}
并且您只能在图像数组计数的限制内滚动。