UIScrollview limit swipe area
Asked Answered
S

3

9

I am trying to limit the swipe area of the UIScrollview, but i amnot able to do that.

I would like to set the swipe area only to the top of the UIScrollview, but i would like to set all the content visible.

Update:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] > 0) {
        UITouch *tempTouch = [touches anyObject];
        CGPoint touchLocation = [tempTouch locationInView:self.categoryScrollView];
        if (touchLocation.y > 280.0)
        {
            NSLog(@"enabled");
            self.categoryScrollView.scrollEnabled = YES;
        }
    }
    [self.categoryScrollView touchesBegan:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [super touchesEnded:touches withEvent:event];
    self.categoryScrollView.scrollEnabled = YES;
    [self.categoryScrollView touchesBegan:touches withEvent:event];
}

Solution: dont forget to set delaysContentTouches to NO on the UIScrollView

self.categoryScrollView.delaysContentTouches = NO;
Surbeck answered 4/12, 2011 at 23:47 Comment(1)
Nice one! 2 yrs later you helped me :PWickerwork
A
7

You can disable scrolling on the UIScrollView, override touchesBegan:withEvent: in your view controller, check if any of the touches began in the area where you'd like to enable swipes, and if the answer is 'yes', re-enable scrolling. Also override touchesEnded:withEvent: and touchesCancelled:withEvent: to disable scrolling when the touches are over.

Arbitrament answered 5/12, 2011 at 0:7 Comment(7)
Hi, something went wrong. The swipe only get enabled after the touch ended, not during the current touching phase:(Millikan
@ViskyMáté I wasn't certain that it would work, but I think it was worth a shot. Another thing to try would be keeping the scroll enabled, disable it in the touchesMoved:withEvent: if they are outside of the area, and re-enable the scroll in the touchesEnded/touchesCancelled.Arbitrament
It is started to work, but:( it is not working all the time. There is a short delay before the touchBegin/Cancel/Moved fired, that's why if i am pinching fast, it doesn't work, any other time it is working. If you can help me to turn of this delay (i dont know where is that) that would be great, and successfull solutionMillikan
@ViskyMáté Well, that's an improvement, I guess. Unfortunately, you cannot turn off the delay in recognition, but you can do one other thing: keep track of all touch events that are currently in progress, and disable scrolling when any of the touches starts outside the designated area. Also I think that it's OK not to pass touchesBegan / touchesEnded / etc. to your categoryScrollView. I know that customizing the scroll may get tricky, but I think you are going in the right direction: I used a similar strategy to implement dragging of objects inside a scroll view.Arbitrament
self.categoryScrollView.delaysContentTouches = NO;Millikan
@ViskyMáté That's a great find! I'll use it in my own code to see if my editor would get more responsive. Thanks!Arbitrament
@dasblinkenlight i know this an old thread but, this is the second time you help me brilliantly. Cheers for you.Wickerwork
T
7

Other answers didn't work for me. Subclassing UIScrollView worked for me (Swift 3):

class ScrollViewWithLimitedPan : UIScrollView {
    // MARK: - UIPanGestureRecognizer Delegate Method Override -
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        let locationInView = gestureRecognizer.location(in: self)
        print("where are we \(locationInView.y)")
        return locationInView.y > 400
    }
}
Tall answered 15/1, 2017 at 18:47 Comment(2)
I think this is the "cleanest" way to go.Stercoricolous
Smooth way it is.Nonaligned
C
4

This blog post showcases a very simple and clean way of implementing the functionality.

// init or viewDidLoad

  UIScrollView *scrollView = (UIScrollView *)view;
  _scrollViewPanGestureRecognzier = [[UIPanGestureRecognizer alloc] init];
  _scrollViewPanGestureRecognzier.delegate = self;
  [scrollView addGestureRecognizer:_scrollViewPanGestureRecognzier];

//

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
 return NO;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
  if (gestureRecognizer == _scrollViewPanGestureRecognzier)
  {
    CGPoint locationInView = [gestureRecognizer locationInView:self.view];
    if (locationInView.y > SOME_VALUE)
    {
      return YES;
    }
    return NO;
  }
  return NO;
}
Consistent answered 26/5, 2014 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.