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;
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.
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
}
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With