IOS手势操作:点击+滑动
【摘要】
1.手指点击事件
//单指单击UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self ...
1.手指点击事件
-
//单指单击
-
UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
-
action:@selector(handleSingleFingerEvent:)];
-
singleFingerOne.numberOfTouchesRequired = 1; //手指数
-
singleFingerOne.numberOfTapsRequired = 1; //tap次数
-
singleFingerOne.delegate = self;
-
-
//单指双击
-
UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
-
action:@selector(handleSingleFingerEvent:)];
-
singleFingerTwo.numberOfTouchesRequired = 1;
-
singleFingerTwo.numberOfTapsRequired = 2;
-
singleFingerTwo.delegate = self;
-
-
//双指单击
-
UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
-
action:@selector(handleDoubleFingerEvent:)];
-
doubleFingerOne.numberOfTouchesRequired = 2;
-
doubleFingerOne.numberOfTapsRequired = 1;
-
doubleFingerOne.delegate = self;
-
-
UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
-
action:@selector(handleDoubleFingerEvent:)];
-
doubleFingerTwo.numberOfTouchesRequired = 2;
-
doubleFingerTwo.numberOfTapsRequired = 2;
-
doubleFingerTwo.delegate = self;
-
-
//如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
-
[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
-
//同理双指亦是如此
-
[doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
-
-
[self.view addGestureRecognizer:singleFingerOne];
-
[self.view addGestureRecognizer:singleFingerTwo];
-
[self.view addGestureRecognizer:doubleFingerOne];
-
[self.view addGestureRecognizer:doubleFingerTwo];
-
-
[singleFingerOne release];
-
[singleFingerTwo release];
-
[doubleFingerOne release];
-
[doubleFingerTwo release];
-
-
//处理单指事件
-
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
-
{
-
if (sender.numberOfTapsRequired == 1) {
-
//单指单击
-
NSLog(@"单指单击");
-
}else if(sender.numberOfTapsRequired == 2){
-
//单指双击
-
NSLog(@"单指双击");
-
}
-
}
-
//处理双指事件
-
- (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender
-
{
-
if (sender.numberOfTapsRequired == 1) {
-
//双指单击
-
NSLog(@"双指单击");
-
}else if(sender.numberOfTapsRequired == 2){
-
//双指双击
-
NSLog(@"双指双击");
-
}
-
}
2.手指滑动事件
-
UISwipeGestureRecognizer *recognizer;
-
-
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
-
-
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
-
[[self view] addGestureRecognizer:recognizer];
-
[recognizer release];
-
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
-
-
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
-
[[self view] addGestureRecognizer:recognizer];
-
[recognizer release];
-
-
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
-
-
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
-
[[self view] addGestureRecognizer:recognizer];
-
[recognizer release];
-
-
-
-
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
-
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
-
[[self view] addGestureRecognizer:recognizer];
-
[recognizer release];
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft||recognizer.direction==UISwipeGestureRecognizerDirectionDown) {
NSLog(@"swipe left");
//执行程序
[self ReadNextPage];
}
if(recognizer.direction==UISwipeGestureRecognizerDirectionRight||recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"swipe right");
//执行程序
[self ReadLastPage];
}
}
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/8630960
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)