我在这里遗漏了一些简单的东西,请帮助我了解一下。
我的控制器设置
UIGestureRecognizer *swipe = [[UIGestureRecognizer alloc]
initWithTarget:gameView action:@selector(handleSwipeFrom:)];
[gameView addGestureRecognizer:swipe];
GameView 已设置
@interface GameView : UIView<UIGestureRecognizerDelegate>
进一步声明
- (IBAction) handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer;
并将其设置为
- (IBAction)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {
NSLog(@" .............. Swipe detected!! ...................");
}
故事板将UIGestureRecognizer
与此IBACtion
相关联,设置如下:
我有点担心UIGestureRecognizer
出现在Controller下方,而不是显示的视图,但我似乎无法纠正它。
构建应用程序时,无法识别滑动。
请建议我在这里缺少什么,以及我是否正确地进行了设置。
答案 0 :(得分:16)
您正在设置两个单独的手势识别器。一个代码可能什么都不做,另一个在故事板中可能没有目标。
在代码中,您应该像这样初始化滑动手势识别器:
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:gameView action:@selector(handleSwipeFrom:)];
// And assuming the "Up" direction in your screenshot is no accident
swipe.direction = UISwipeGestureRecognizerDirectionUp;
或者,当然,您可以在故事板中连接滑动识别器。通过右键单击手势识别器并将SentActions连接到handleSwipeFrom:
方法可以轻松完成此操作,或者类似地,您可以从右侧的连接检查器(如屏幕截图所示)拖动到指定的函数。在图片中,您可以看到我已将Sent Actions
连接到方法swipeTarget:
。
但目前你有两个不完整的识别器。
答案 1 :(得分:12)
-(void)addSwipeEvent:(UIView*)subView{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[subView addGestureRecognizer:recognizer];
[recognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
leftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
leftRecognizer.numberOfTouchesRequired = 1;
leftRecognizer.delegate = self;
[subView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
downRecognizer.direction=UISwipeGestureRecognizerDirectionDown;
downRecognizer.numberOfTouchesRequired = 1;
donwRecognizer.delegate = self;
[subView addGestureRecognizer:downRecognizer];
[downRecognizer release];
UISwipeGestureRecognizer *upRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
upRecognizer.direction=UISwipeGestureRecognizerDirectionUp;
upRecognizer.numberOfTouchesRequired = 1;
upRecognizer.delegate = self;
[subView addGestureRecognizer:upRecognizer];
[upRecognizer release];
}
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
if ( sender.direction == UISwipeGestureRecognizerDirectionLeft ){
NSLog(@" *** SWIPE LEFT ***");
}
if ( sender.direction == UISwipeGestureRecognizerDirectionRight ){
NSLog(@" *** SWIPE RIGHT ***");
}
if ( sender.direction== UISwipeGestureRecognizerDirectionUp ){
NSLog(@" *** SWIPE UP ***");
}
if ( sender.direction == UISwipeGestureRecognizerDirectionDown ){
NSLog(@" *** SWIPE DOWN ***");
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIView class]])
{
return YES;
}
return NO;
}
答案 2 :(得分:1)
UISwipeGestureRecognizer *swipe = [[UISwipGestureRecognizer alloc]
initWithTarget:gameView action:@selector(handleSwipeFrom:)];
[gameView addGestureRecognizer:swipe];
- (IBAction)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {
NSLog(@" .............. Swipe detected!! ...................");
}
然后撤消你在IB中所做的事情。如果你在代码中执行它,那么在IB中执行它也只是复制工作,并且可能会加倍滑动处理的次数