手势的应用代码示例
如何为view添加手势以及对手势做出响应呢
为view添加手势首先定义一个手势对象
UISwipeGestureRecognizer * recognizer;
2.给view添加上四种手势的代码如下:
添加右滑手势:
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [self.view addGestureRecognizer:recognizer];
添加左滑手势:
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [self.view addGestureRecognizer:recognizer];
添加上滑手势:
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; [self.view addGestureRecognizer:recognizer];
添加下滑手势:
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@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"); }}
# 作者邮件:zhangxmsy@163.com, 有问题联系。
网友评论