I have a main UIView, which contains a scrollview. I have set up UIGestureRecognizer for the main view for 4 types of swipes using this code:
UISwipeGestureRecognizer *swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(upCommand)];
[swipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[mainGameView addGestureRecognizer:swipeUpRecognizer];
... // Done 4 times for each direction
When I disable scrolling on the scrollview, this code works great (I can swipe anywhere on the screen and the associated actions perform as expected). However, I want to add functionality so that if I touch two fingers on the scrollview, I can pan back and forth as a scrollview usually functions. I tried adding a gesture recognizer to the scrollview to detect when two fingers pan:
- (void)viewDidLoad
{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(recognizePan)];
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[scrollView addGestureRecognizer:panGestureRecognizer];
}
- (void)recognizePan
{
[gameScrollView setScrollEnabled:YES];
}
I used this in conjunction with the following method to disable scrolling again once the fingers are lifted:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[gameScrollView setScrollEnabled:NO];
}
This kind of worked, but not in the way I want it to. When I drag two fingers on the scrollview, scrolling would be set to enabled, but I can't use those two fingers to scroll. I first need to lift the two fingers and then I am able to scroll with one finger (two fingers don't work). And when I lift up the single finger that could scroll through the scrollview, scrolling is disabled, as set in scrollViewDidEndDragging
.
Obviously, this type of scrolling would not be very user-friendly, but I can't seem to find a way to set the scrollview so it only scrolls when two fingers are dragging on the scrollview. Thanks for any help in advance.
~ 17-year-old amateur iOS developer & new to gestures
EDIT: As per one of the suggestions from this question, I have tried implementing a subclass of UISubView to override the default touchesBegan method, but I haven't been able to get it to work.
CustomScrollView.h:
@interface CustomScrollView : UIScrollView
{
}
@end
CustomScrollView.m:
#import "CustomScrollView.h"
@implementation CustomScrollView
- (id)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
// What goes here so that The action it can be called from the ViewController.h
}
@end
ViewController.h:
#import <UIKit/UIKit.h>
@class CustomScrollView;
@interface ViewController : UIViewController <UIScrollViewDelegate>
{
CustomScrollView *scrollView;
}
@end
ViewController.m:
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// What goes here?
}