I have a Button1 outside ScrollView and another Button2 inside scrollview. I am using storyboard. I have used drag drop segue from both buttons to a different view. Button1 works fine, the problem is with Button2, it doesnt work no matter how many times I click, it only works when I click and drag (strange!!). When I troubleshooted I found that whenever I create an outlet of scrollview it behaves this way, if I remove the connection of scrollview to the outlet it works fine, but I need the outlet for the scrollview to work. Any solution on how I can get this done? This is a sample code of my view did load if it helps
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
self.scrollView.contentSize =CGSizeMake(320, 500);
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignKeyboard)];
[self.scrollView addGestureRecognizer:singleTap];
singleTap.numberOfTapsRequired = 1;
}
-(void) resignKeyboard
{
[self.view endEditing:YES];
}
This is how I defined the outlet
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
Note that Button2 Click does work but on click and drag/swipe not on single or multiple clicks.
EDIT:I checked and found that it works fine for simulator 6.1 but not for 5.0
I figured out the problem was because of adding tap gesture, because when your button is inside scrollview the first tap it works for scrollview and not button, so all I had to do is check if touch is for button or not. Here is the code that fixed this issue.
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (self.scrollView.superview != nil) {
if ([touch.view isKindOfClass:[UIButton class]])
{
return NO; // ignore the touch
}
}
return YES; // handle the touch
}
Add delegate UIGestureRecognizerDelegate and in viewdidload add this line in the end
singleTap.delegate = self;