




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
iOS手勢識別的詳細使用(拖動,縮放,旋轉(zhuǎn),點擊,手勢依賴,自定義手勢)1、UIGestureRecognizer介紹手勢識別在iOS上非常重要,手勢操作移動設(shè)備的重要特征,極大的增加了移動設(shè)備使用便捷性。iOS系統(tǒng)在3.2以后,為方便開發(fā)這使用一些常用的手勢,提供了UIGestureRecognizer類。手勢識別UIGestureRecognizer類是個抽象類,下面的子類是具體的手勢,開發(fā)這可以直接使用這些手勢識別。UITapGestureRecognizerUIPinchGestureRecognizerUIRotationGestureRecognizerUISwipeGestureRecognizerUIPanGestureRecognizerUILongPressGestureRecognizer上面的手勢對應(yīng)的操作是:Tap(點一下)Pinch(二指往內(nèi)或往外撥動,平時經(jīng)常用到的縮放)Rotation(旋轉(zhuǎn))Swipe(滑動,快速移動)Pan(拖移,慢速移動)LongPress(長按)UIGestureRecognizer的繼承關(guān)系如下:-Q_ UlFont J)|p(LJJLcngPressGestureReco^niaefc、UiGeslureRecognizerIfUIPanGestureRecognizer]、 UHmage )UIPinchGeslureRecogniizer乂UlLocalizedhdexedColiation')-QJlFlotationGestureReGognizer■yUlLocalNotificationj〈UlSwipeGestureFlecognizer、UlMenuContrailer)£UITapGestureRecogriize<」[ NSObject J—-( UlMenultem )2、 使用手勢的步驟使用手勢很簡單,分為兩步:創(chuàng)建手勢實例。當創(chuàng)建手勢時,指定一個回調(diào)方法,當手勢開始,改變、或結(jié)束時,回調(diào)方法被調(diào)用。2-添加到需要識別的View中。每個手勢只對應(yīng)一個View,當屏幕觸摸在View的邊界內(nèi)時,如果手勢和預(yù)定的一樣,那就會回調(diào)方法。ps:—個手勢只能對應(yīng)一個View,但是一個View可以有多個手勢。建議在真機上運行這些手勢,模擬器操作不太方便,可能導致你認為手勢失效。3、 Pan拖動手勢:[cpp]viewplaincopyUllmageView*snakeImageView=[[UllmageViewalloc]initWithImage:[UIImageimageNamed:@"snake.png"]];snakelmageView.frame=CGRectMake(50,50,100,160);UIPanGestureRecognizer*panGestureRecognizer=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePan:)];[snakeImageViewaddGestureRecognizer:panGestureRecognizer];[self.viewsetBackgroundColor:[UIColorwhiteColor]];[self.viewaddSubview:snakeImageView];新建一個ImageView,然后添加手勢回調(diào)方法:[cpp]viewplaincopy一(void)handlePan:(UIPanGestureRecognizer*)recognizer{CGPointtranslation=[recognizertranslationInView:self.view];recognizer.view.center=CGPointMake(recognizer.view.center.x+translation.x,recognizer.view.center.y+translation.y);[recognizersetTranslation:CGPointZeroinView:self.view];7.8?}4、Pinch縮放手勢[cpp]viewplaincopyUIPinchGestureRecognizer*pinchGestureRecognizer=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePinch:)];<pclass="p1">[<spanclass="s1"〉snakeImageView</span><spanclass="s2"〉addGestureRecognizer</span〉:pinchGestureRecognizer];</p>[cpp]viewplaincopy1.一(void)handlePinch:(UIPinchGestureRecognizer*)recognizer2?{recognizer.view.transform=CGAffineTransformScale(recognizer.view.transform,recognizer.scale,recognizer.scale);recognizer.scale=1;5?}5、Rotation旋轉(zhuǎn)手勢[cpp]viewplaincopy1.UIRotationGestureRecognizer*rotateRecognizer=[[UIRotationGestureRecognizeralloc]2.3.initWithTarget:self2.3.action:@selector(handleRotate:)];[snakelmageViewaddGestureRecognizer:rotateRecognizer];[cpp]viewplaincopy1.一(void)handleRotate:(UIRotationGestureRecognizer*)recognizer2?{recognizer.view.transform=CGAffineTransformRotate(recognizer.view.transform,recognizer.rotation);recognizer.rotation=0;6二■V/~/\\\\□CarrierW 4:21PM添加了這幾個手勢后,運行看效果,程序中的6二■V/~/\\\\□CarrierW 4:21PM添加了這幾個手勢后,運行看效果,程序中的imageview放了一個/W\」_10|'\\\的圖片,在模擬器上拖動是沒問題的??s放和旋轉(zhuǎn)有點問題,估計是因為在模擬器上的模擬的兩個接觸點距離在imageView的邊界外了,所以操作無效果。建議在真機上運行這個手勢。在模擬器上縮放和選擇的操作技巧:可以把imageView的frame值設(shè)置大一點,按住alt鍵,按下觸摸板(不按下不行),這樣就可以旋轉(zhuǎn)和縮放了。6、添加第二個ImagView并添加手勢記?。阂粋€手勢只能添加到一個View,兩個View當然要有兩個手勢的實例了[cpp]viewplaincopy-(void)viewDidLoad{[superviewDidLoad];4.UIImageView*snakeImageView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"snake.png"]];UIImageView*dragonImageView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"dragon.png"]];snakeImageView.frame=CGRectMake(120,120,100,160);dragonImageView.frame=CGRectMake(50,50, 100,160);[self.viewaddSubview:snakeImageView];[self.viewaddSubview:dragonImageView];11.for(UIView*viewinself.view.subviews) {UIPanGestureRecognizer*panGestureRecognizer=[[UIPanGestureRecognizeralloc]
14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.initWithTarget:selfaction:@selector(handlePan:)];UIPinchGestureRecognizer*pinchGestureRecognizer=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePinch:)];UIRotationGestureRecognizer*rotateRecognizer=[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleRotate:)];[viewaddGestureRecognizer:panGestureRecognizer];[viewaddGestureRecognizer:pinchGestureRecognizer];[viewaddGestureRecognizer:rotateRecognizer];[viewsetUserInteractionEnabled:YES];}[self.viewsetBackgroundColor:[UIColorwhiteColor]];}多添加了一條龍的view,兩個view都能接收上面的三種手勢。運行效果如下:
7、拖動(pan手勢)速度(以較快的速度拖放后view有滑行的效果)如何實現(xiàn)呢?監(jiān)視手勢是否結(jié)束監(jiān)視觸摸的速度[cpp]viewplaincopy一(void)handlePan:(UIPanGestureRecognizer*)recognizer{CGPointtranslation=[recognizertranslationInView:self.view];recognizer.view.center=CGPointMake(recognizer.view.center.x+translation.x,recognizer.view.center.y+translation?y);[recognizersetTranslation:CGPointZeroinView:self.view];7if(recognizer.state==UIGestureRecognizerStateEnded){9.CGPointvelocity=[recognizervelocityInView:self.view];CGFloatmagnitude=sqrtf((velocity.x*velocity.x)+(velocity.y*velocity.y));CGFloatslideMult=magnitude/200;NSLog(@"magnitude:%f,slideMult:%f",magnitude,slideMult);14.floatslideFactor=0.1*slideMult;//IncreaseformoreofaslideCGPointfinalPoint=CGPointMake(recognizer.view.center.x+(velocity.x*slideFactor),recognizer.view.center.y+(velocity.y*slideFactor));finalPoint.x=MIN(MAX(finalPoint.x,0),self.view.bounds.size.width);finalPoint.y=MIN(MAX(finalPoint.y,0),self.view.bounds.size.height);20.[UIViewanimateWithDuration:slideFactor*2delay:0options:UIViewAnimationOptionCurveEaseOutanimations:^recognizer.view.center=finalPoint;}completion:nil];24.}26.代碼實現(xiàn)解析:計算速度向量的長度(估計大部分都忘了)這些知識了。如果速度向量小于200,那就會得到一個小于的小數(shù),那么滑行會很短基于速度和速度因素計算一個終點確保終點不會跑出父View的邊界5-使用UIView動畫使view滑動到終點運行后,快速拖動圖像view放開會看到view還會在原來的方向滑行一段路。&同時觸發(fā)兩個view的手勢手勢之間是互斥的,如果你想同時觸發(fā)蛇和龍的view,那么需要實現(xiàn)協(xié)議
UIGestureRecognizerDelegate,[cpp]viewplaincopy?interfaceViewController:UIViewController<UIGestureRecognizerDelegate>@end并在協(xié)議這個方法里返回YES。[cpp]viewplaincopy-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{returnYES;}把self作為代理設(shè)置給手勢:[cpp]viewplaincopypanGestureRecognizer.delegate=self;pinchGestureRecognizer.delegate=self;rotateRecognizer.delegate=self;這樣可以同時拖動或旋轉(zhuǎn)縮放兩個view了。9、tap點擊手勢這里為了方便看到tap的效果,當點擊一下屏幕時,播放一個聲音。為了播放聲音,我們加入AVFoundation.framework這個框架。[cpp]viewplaincopy一(AVAudioPlayer*)loadWav:(NSString*)filename{NSURL*url=[[NSBundlemainBundle]URLForResource:filenamewithExtension:@"wav"];NSError*error;AVAudioPlayer*player=[[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:&error];if(!player){NSLog(@"Errorloading%@:%@",url,error.localizedDescription);}else {[playerprepareToPlay];9? }10. return player;
}我會在最后例子代碼給出完整代碼,添加手勢的步驟和前面一樣的。[cpp]viewplaincopy#import<UIKit/UIKit.h〉#import<AVFoundation/AVFoundation.h〉3.?interfaceViewController:UIViewController<UIGestureRecognizerDelegate>@property(strong)AVAudioPlayer*chompPlayer;@property(strong)AVAudioPlayer*hehePlayer;7.@end[cpp]viewplaincopy一(void)handleTap:(UITapGestureRecognizer*)recognizer{[self.chompPlayerplay];3?}運行,點一下某個圖,就會播放一個咬東西的聲音。不過這個點擊播放聲音有點缺陷,就是在慢慢拖動的時候也會播放。這使得兩個手勢重合了。怎么解決呢?使用手勢的:requireGestureRecognizerToFail方法。10、 手勢的依賴性在viewDidLoad的循環(huán)里添加這段代碼:[cpp]viewplaincopy[tapRecognizerrequireGestureRecognizerToFail:panGestureRecognizer];意思就是,當如果pan手勢失敗,就是沒發(fā)生拖動,才會出發(fā)tap手勢。這樣如果你有輕微的拖動,那就是pan手勢發(fā)生了。tap的聲音就不會發(fā)出來了。11、 自定義手勢自定義手勢繼承:UIGestureRecognizer,實現(xiàn)下面的方法:[cpp]viewplaincopy—touchesBegan:withEvent:—touchesMoved:withEvent:—touchesEnded:withEvent:一touchesCancelled:withEvent:
新建一個類,繼承UIGestureRecognizer,代碼如下:.h文件[cpp]viewplaincopy#import<UIKit/UIKit.h〉typedefenum{Directionunknown =0,DirectionLeft,DirectionRight}Direction;7.8.?interfaceHappyGestureRecognizer:UIGestureRecognizer?property(assign)?property(assign)?property(assign)?property(assign)?property(assign)inttickleCount;CGPointcurTickleStart;DirectionlastDirection;12.13.@end.m文件[cpp]viewplaincopy#import"HappyGestureRecognizer.h"#import<UIKit/UIGestureRecognizerSubclass.h>#defineREQUIRED_TICKLES 2#defineMOVE_AMT_PER_TICKLE 255.?implementationHappyGestureRecognizer7.-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event{UITouch*touch=[touchesanyObject];self.curTickleStart=[touchlocationInView:self.view];}12.13.14.15.16.17.13.14.15.16.17.18.19.20.21.22.(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event{//Makesurewe'vemovedaminimumamountsincecurTickleStartUITouch*touch=[touchesanyObject];CGPointticklePoint=[touchlocationInView:self.view];CGFloatmoveAmt=ticklePoint.x-self.curTickleStart.x;DirectioncurDirection;if(moveAmt<0) {curDirection=DirectionLeft;}else{24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.4 6.47.48.49.50.51.52.53.54.55.5 6.57.58.59.60.61.62.63.64.}if(ABS(moveAmt)<MOVE_AMT_PER_TICKLE)return;//確認方向改變了if(self.lastDirection==Directionunknown||(self.lastDirection==DirectionLeft&&curDirection==DirectionRight)||(self.lastD
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 公路ppp合同范本
- 分紅比例合同范本
- 公路規(guī)劃合同范本
- 協(xié)議合同范本寫法
- 兼職還款合同范本
- pos機推廣合同范本
- 入股店鋪協(xié)議合同范本
- 義齒加工合同范本模板
- 京東入職合同范本
- 醫(yī)院整體轉(zhuǎn)讓合同范本
- 安全生產(chǎn)責任制考核制度和考核表(完整版)
- 19J102-1 19G613混凝土小型空心砌塊墻體建筑與結(jié)構(gòu)構(gòu)造
- 建筑垃圾清運及處置 投標方案(技術(shù)方案)
- 2024年常州信息職業(yè)技術(shù)學院單招職業(yè)技能測試題庫及答案解析
- 《中國陶瓷史》課件-1-中國陶瓷史概述
- 英語教師課堂提問省公開課一等獎全國示范課微課金獎?wù)n件
- 經(jīng)皮式氣管切開術(shù)
- 2024嘉興市城南街道招聘筆試參考題庫附帶答案詳解
- 個人維修收款收據(jù)
- 代辦電瓶車車牌照委托書
- 智慧農(nóng)業(yè)中的智能農(nóng)機與農(nóng)具技術(shù)
評論
0/150
提交評論