0

I have a scrollView of 280(w) x 350(h) and a content view of 840(w) x 405(h).

I switch between the views with a segmented control like this:

- (IBAction)segmentedClicked:(UISegmentedControl *)sender
{
    CGFloat x = sender.selectedSegmentIndex * self.personalDetailsScrollView.frame.size.width;
    CGPoint point = CGPointMake(x, 0);
    [self.personalDetailsScrollView setContentOffset:point animated:YES];
}

I want to disable the regular horizontal scrolling so only the segmented buttons will scroll the view horizontally. The vertical scroll should stay active.

Tried to use -(void)scrollViewDidScroll and the solution offered here: (How to lock the horizontal scrolling of a scrollView in iOS) but it didn't work for me for some reason.

Community
  • 1
  • 1
MatanGold
  • 739
  • 10
  • 18

4 Answers4

2

I haven't tried it, but this is the approach I'd take:

In UIScrollView, there is a property:

@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer

So I'd make my own new UIPanGestureRecognizer extension and override:

- (CGPoint)translationInView:(UIView *)view
{
    CGPoint tmp = [super translationInView:view];
    return CGPointMake(0, tmp.y);
}

Add that gesture recognizer to the scrollview, then call:

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:youCustomOne];

It should work.

mprivat
  • 21,582
  • 4
  • 54
  • 64
0

try this

- (IBAction)segmentedClicked:(UISegmentedControl *)sender
{
    if(sender.selectedSegmentIndex == 0)
    {
       CGPoint point = CGPointMake(0, 0);
    }
    else
    {
       CGPoint point = CGPointMake(self.personalDetailsScrollView.frame.size.width, 0);
     }
    [self.personalDetailsScrollView setContentOffset:point animated:YES];
    }
NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

I know this is a late answer, but I see there is still no accepted answer, so I'll give it a go.

It sounds like what you are trying to do is use 1 controller to effectively hold 3 separate screens. You're trying to switch between the screens depending on what the user selects in a UISegmentedControl, and you don't want the user to be able to pan between the views by just horizontally swiping. Why not just have 3 different view controllers? I think that is the more expected way to setup what you are trying to do, and therefore you will find it much easier to implement.

GeneralMike
  • 2,951
  • 3
  • 28
  • 56
0

Swift 3, Set UIScrollViewDelegate to your class and then use below code.

scrollView.isDirectionalLockEnabled = true

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.x>0 {
            webView.scrollView.contentOffset.x = 0.0
        }
    }
ZAFAR007
  • 3,049
  • 1
  • 34
  • 45