日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iPhone手势处理--UIGestureRecognizer

發(fā)布時(shí)間:2023/12/18 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iPhone手势处理--UIGestureRecognizer 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、概述

iPhone中處理觸摸屏的操作,在3.2之前是主要使用的如下4種方式:

?- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
?- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
?- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
?- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

但是這種方式比較麻煩,目前有了比較簡(jiǎn)便的方式,就是使用UIGestureRecognizer。

二、UIGestureRecognizer

UIGestureRecognizer是一個(gè)抽象類,我們主要是使用它的子類:

  • UITapGestureRecognizer

  • UIPinchGestureRecognizer

  • UIRotationGestureRecognizer

  • UISwipeGestureRecognizer

  • UIPanGestureRecognizer

  • UILongPressGestureRecognizer

從名字上我們就能知道, Tap(點(diǎn)一下)、Pinch(二指往內(nèi)或往外撥動(dòng))、Rotation(旋轉(zhuǎn))、Swipe(滑動(dòng),快速移動(dòng))、Pan (拖移,慢速移動(dòng))以及 LongPress(長(zhǎng)按)。舉個(gè)例子,可以在viewDidLoad函數(shù)里面添加:

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[self.view addGestureRecognizer:panRecognizer];

panRecognizer.maximumNumberOfTouches = 1;
panRecognizer.delegate = self;
[panRecognizer release];

其它手勢(shì)方法類似。主要就是設(shè)置delegate和在需要的view上使用addGestureRecognizer。當(dāng)然在要記得在作為delegate的view的頭文件加上<UIGestureRecognizerDelegate>。

不過有些手勢(shì)是關(guān)聯(lián)的,怎么辦呢?例如 Tap 與 LongPress、Swipe與 Pan,或是 Tap 一次與Tap 兩次。比如單擊和雙擊,如果它識(shí)別出一種手勢(shì),其后的手勢(shì)將不被識(shí)別,也就是說單擊和雙擊并存時(shí),它只能發(fā)送出單擊的消息,這個(gè)時(shí)候就需要先判斷是否時(shí)雙擊,在雙擊失效的情況下作為單擊。

requireGestureRecognizerToFail,他可以指定某一個(gè) recognizer,即便自己已經(jīng)滿足條件了,也不會(huì)立刻觸發(fā),會(huì)等到該指定的 recognizer 確定失敗之后才觸發(fā)。

-?(void)viewDidLoad?{ // 單擊的 RecognizerUITapGestureRecognizer*?singleRecognizer;singleRecognizer?=?[[UITapGestureRecognizer?alloc]?initWithTarget:selfaction:@selector(handleSingleTapFrom)];singleTapRecognizer.numberOfTapsRequired?=?1;?// 單擊[self.view?addGestureRecognizer:singleRecognizer];// 雙擊的 RecognizerUITapGestureRecognizer*?double;doubleRecognizer?=?[[UITapGestureRecognizer?alloc]?initWithTarget:selfaction:@selector(handleDoubleTapFrom)];doubleTapRecognizer.numberOfTapsRequired?=?2;?// 雙擊[self.view?addGestureRecognizer:doubleRecognizer];// 關(guān)鍵在這一行,如果雙擊確定偵測(cè)失敗才會(huì)觸發(fā)單擊[singleRecognizer?requireGestureRecognizerToFail:doubleRecognizer];[singleRecognizer?release];[doubleRecognizer?release];} 參考資料:
http://www.cocoachina.com/iphonedev/sdk/2010/1214/2471.html

http://blog.csdn.net/pjk1129/article/details/6824810




總結(jié)

以上是生活随笔為你收集整理的iPhone手势处理--UIGestureRecognizer的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。