手势的应用代码示例
- 为view添加手势首先定义一个手势对象
UISwipeGestureRecognizer * recognizer;
2.给view添加上四种手势的代码如下:
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.view addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.view addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self.view addGestureRecognizer:recognizer];
- 添加手势的响应事件:
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
if(recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"swipe down");
}
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"swipe up");
}
if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"swipe left");
}
if(recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"swipe right");
}
}