Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollview limit swipe area

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;
like image 931
Visky Máté Avatar asked Dec 04 '11 23:12

Visky Máté


3 Answers

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.

like image 152
Sergey Kalinichenko Avatar answered Oct 09 '22 13:10

Sergey Kalinichenko


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
    }
}
like image 28
Dan Rosenstark Avatar answered Oct 09 '22 13:10

Dan Rosenstark


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;
}
like image 25
Vlad Smoc Avatar answered Oct 09 '22 13:10

Vlad Smoc