私は ViewController (tableView を持つ) に、次々に動作する必要がある 2 つのジェスチャ認識機能を実装しようとしています。1 つ目は下にスワイプするジェスチャで、2 つ目は長押しジェスチャです。
@sergioの提案で変更された私のコードは次のとおりです
- (void)viewDidLoad
{
[super viewDidLoad];
swipeDown = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDownAction)] autorelease];
longPress = [[[CustomLongPress alloc]initWithTarget:self action:@selector(longPressAction)] autorelease];
longPress.minimumPressDuration = 2;
swipeDown.numberOfTouchesRequired = 1;
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate = self ;
longPress.delegate = self ;
[myTableView addGestureRecognizer:swipeDown];
[myTableView addGestureRecognizer:longPress];
}
-(void)swipeDownAction {
_methodHasBeenCalled = YES; // bool @property declared in .h
NSLog(@"Swipe down detected");
}
-(void)longPressAction {
NSLog(@"long press detected");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
そして私の UILongPressGestureRecognizer サブクラス:
#import "CustomLongPress.h"
#import "ViewController.h"
@interface CustomLongPress()
{
ViewController *vc;
}
@end
@implementation CustomLongPress
-(id)initWithTarget:(id)target action:(SEL)action controller:(ViewController *)viewCon
{
self = [super initWithTarget:target action:action];
if (self) {
vc = viewCon;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(vc.methodHasBeenCalled ? @"Yes" : @"No");
if (vc.methodHasBeenCalled) {
[super touchesBegan:touches withEvent:event];
}
}
残念ながら、私はまだ swipeDown からのログのみを取得しますが、longPress に関してはログを取得しません