I have a vertical UIScrollView that has several large buttons taking up most of the space in the scroll view. For this reason I've had to subclass the scroll view following this post ios 8 - buttons in horizontal scroll view intercepting pan event - scroll does not work) and override the following class method:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
static int count = 0;
count++;
NSLog(@"calling this function 2");
NSLog(@"count = %d", count);
UITouch *touch = [touches anyObject];
if(touch.phase == UITouchPhaseMoved)
{
NSLog(@"UITouchPhaseMoved so returning no");
return NO;
}
else
{
NSLog(@"returning super");
NSLog(@"returning whatever this value is %d 0/1", [super touchesShouldBegin:touches withEvent:event inContentView:view]);
[super touchesShouldBegin:touches withEvent:event inContentView:view];
return [super touchesShouldBegin:touches withEvent:event inContentView:view];
}
}
When I run my app, this above function is called when a button is tapped but the function is not called when I drag anywhere in the scroll view, inside or outside a button. And the scroll view doesn't scroll regardless of where I try to drag.
Here's all my code related to the scroll view (it's set up with IB):
smallView.scrollEnabled = YES;
smallView.contentSize = CGSizeMake(smallView.frame.size.width, smallView.frame.size.height*1.4);
smallView.delegate = self;
And here's the buttons-related code in case that matters:
NSArray *buttonsArray = @[button1, button2, button3, button4, button5];
for (int i = 0; i < [buttonsArray count]; i++) {
UIButton *button = buttonsArray[i];
button.tag = i+1;
[button addTarget:self action:@selector(p_motivationButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
}
Why is my scroll view not scrolling and not even calling touchesShouldBegin
?