UITapGestureRecognizer 的用法(轻触手势识别器)
最近在项目中用到了手势操作,键盘回收时还是挺常用的,现在总结下,多谢网络上大神们的分享。
先分享下我在项目中用的代码:
uitapgesturerecognizer * mytap=[[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(tap_gesturerecognizer:)]; [self addgesturerecognizer:mytap]; [mytap release]; //收回键盘 -(void)tap_gesturerecognizer:(uitapgesturerecognizer *)tap_gest { [self.scv_stockpricetextfield resignfirstresponder]; [self.scv_stockamouttextfield resignfirstresponder]; }
//单指单击 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];
处理事件的方法,代码: 1: //处理单指事件 2: - (void)handlesinglefingerevent:(uitapgesturerecognizer *)sender 3: { 4: if (sender.numberoftapsrequired == 1) { 5: //单指单击 6: nslog(@"单指单击"); 7: }else if(sender.numberoftapsrequired == 2){ 8: //单指双击 9: nslog(@"单指双击"); 10: } 11: } 12: //处理双指事件 13: - (void)handledoublefingerevent:(uitapgesturerecognizer *)sender 14: { 15: if (sender.numberoftapsrequired == 1) { 16: //双指单击 17: nslog(@"双指单击"); 18: }else if(sender.numberoftapsrequired == 2){ 19: //双指双击 20: nslog(@"双指双击"); 21: } 22: }
将相应代码复制到你的工程中即可使用,由于代码中已经有详细的解释说明,这里就不在重复解释了。
代码中只是列举了单指与双指对于单击或多击的处理,同理多指的操作需修改numberoftouchesrequired属性,对点击的次数需修改numberoftapsrequired属性。
对于其他手势例如uiswipegesturerecognizer,uilongpressgesturerecognizer,uilongpressgesturerecognizer 的操作使用类似处理。
uikit中包含了uigesturerecognizer类,用于检测发生在设备中的手势。uigesturerecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势:
1、拍击uitapgesturerecognizer (任意次数的拍击)
2、向里或向外捏uipinchgesturerecognizer (用于缩放)
3、摇动或者拖拽uipangesturerecognizer
4、擦碰uiswipegesturerecognizer (以任意方向)
5、旋转uirotationgesturerecognizer (手指朝相反方向移动)
6、长按uilongpressgesturerecognizer
对于不同类型的手势识别器,具有不同的配置属性。比如uitapgesturerecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一 个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。