iOS手势识别的详细使用_第1页
iOS手势识别的详细使用_第2页
iOS手势识别的详细使用_第3页
iOS手势识别的详细使用_第4页
iOS手势识别的详细使用_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)1、UIGestureRecognizer介绍手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别。UITapGestureRecognizerUIPinchGestureRecognizerUIRotationGestureRecognizerUISwipeGestureRecognizerUIPanGestureRecognizerUILongPressGestureRecognizer上面的手势对应的操作是:Tap(点一下)Pinch(二指往內或往外拨动,平时经常用到的缩放)Rotation(旋转)Swipe(滑动,快速移动)Pan(拖移,慢速移动)LongPress(长按)UIGestureRecognizer的继承关系如下:-Q_ UlFont J)|p(LJJLcngPressGestureReco^niaefc、UiGeslureRecognizerIfUIPanGestureRecognizer]、 UHmage )UIPinchGeslureRecogniizer乂UlLocalizedhdexedColiation')-QJlFlotationGestureReGognizer■yUlLocalNotificationj〈UlSwipeGestureFlecognizer、UlMenuContrailer)£UITapGestureRecogriize<」[ NSObject J—-( UlMenultem )2、 使用手势的步骤使用手势很简单,分为两步:创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。2-添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。ps:—个手势只能对应一个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,然后添加手势回调方法:[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旋转手势[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|'\\\的图片,在模拟器上拖动是没问题的。缩放和旋转有点问题,估计是因为在模拟器上的模拟的两个接触点距离在imageView的边界外了,所以操作无效果。建议在真机上运行这个手势。在模拟器上缩放和选择的操作技巧:可以把imageView的frame值设置大一点,按住alt键,按下触摸板(不按下不行),这样就可以旋转和缩放了。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有滑行的效果)如何实现呢?监视手势是否结束监视触摸的速度[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.代码实现解析:计算速度向量的长度(估计大部分都忘了)这些知识了。如果速度向量小于200,那就会得到一个小于的小数,那么滑行会很短基于速度和速度因素计算一个终点确保终点不会跑出父View的边界5-使用UIView动画使view滑动到终点运行后,快速拖动图像view放开会看到view还会在原来的方向滑行一段路。&同时触发两个view的手势手势之间是互斥的,如果你想同时触发蛇和龙的view,那么需要实现协议

UIGestureRecognizerDelegate,[cpp]viewplaincopy©interfaceViewController:UIViewController<UIGestureRecognizerDelegate>@end并在协议这个方法里返回YES。[cpp]viewplaincopy-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{returnYES;}把self作为代理设置给手势:[cpp]viewplaincopypanGestureRecognizer.delegate=self;pinchGestureRecognizer.delegate=self;rotateRecognizer.delegate=self;这样可以同时拖动或旋转缩放两个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的循环里添加这段代码:[cpp]viewplaincopy[tapRecognizerrequireGestureRecognizerToFail:panGestureRecognizer];意思就是,当如果pan手势失败,就是没发生拖动,才会出发tap手势。这样如果你有轻微的拖动,那就是pan手势发生了。tap的声音就不会发出来了。11、 自定义手势自定义手势继承:UIGestureRecognizer,实现下面的方法:[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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论