2015-10-15 99 views
3

我正在实现具有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天的这个问题,但无法解决。 任何帮助,可以赞赏。 谢谢。

回答

1

尝试重置的UIScrollViewcontentOffset,如果这是错误的方向(或禁用方向)

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    if (scrollView.contentOffset.x > stopPosition) { // or (scrollView.contentOffset.x < stopPosition) 
     [scrollView setContentOffset:CGPointMake(stopPosition, 0)]; 
    } 
} 
+0

您好anthu谢谢您的回复。只是为了澄清我正在实现水平滚动,但你更新了垂直滚动的y位置。为什么 – lazyCoder

+0

对不起,我没注意到。但我希望你能理解我的答案。我编辑了我的答案。 – anhtu

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]; 
} 

你将只能在极限滚动你的图像数组数。

+0

嗨ErmineSoft我会实施你的建议,让你知道谢谢 – lazyCoder

相关问题