配色: 字号:
iOS开发之触摸事件以及手势
2016-12-08 | 阅:  转:  |  分享 
  
iOS开发之触摸事件以及手势



这篇文章主要为大家详细介绍了iOS开发之触摸事件以及手势的相关资料,感兴趣的小伙伴们可以参考一下



iOS中的事件分为三类:触摸事件、加速计事件、远程控制事件。只有继承了UIResponder的对象才能接收并处理事件,称之为“响应者对象”。UIApplication、UIViewController、UIView都继承自UIResponder。UIResponder内部提供的方法来处理事件:



触摸事件:touchesBegan、touchesMoved、touchesEnded、touchesCancelled



加速计事件:motionBegan、motionEnded、motionCancelled



远程控制事件:remoteControlReceivedWithEvent



UIVeiw的触摸事件处理过程:



/当手指开始触摸view时调用@paramtouches<#touchesdescription#>@paramevent<#eventdescription#>/-(void)touchesBegan:(NSSet)toucheswithEvent:(UIEvent)event{NSLog(@"%s",__func__);}/当手指在view上移动时调用@paramtouches<#touchesdescription#>@paramevent<#eventdescription#>/-(void)touchesMoved:(NSSet)toucheswithEvent:(UIEvent)event{NSLog(@"%s",__func__);}/当手指离开view时调用@paramtouches<#touchesdescription#>@paramevent<#eventdescription#>/-(void)touchesEnded:(NSSet)toucheswithEvent:(UIEvent)event{NSLog(@"%s",__func__);}/当触摸事件被系统事件打断时调用@paramtouches<#touchesdescription#>@paramevent<#eventdescription#>/-(void)touchesCancelled:(NSSet)toucheswithEvent:(UIEvent)event{NSLog(@"%s",__func__);}



一次触摸动作必然会调用touchesBeagn、touchesMoved和touchesEnded这三个方法。



说到这几个触摸方法,首先要知道UITouch这个对象。当一根手指触摸屏幕时就会产生一个与之关联的UITouch对象,一根手指对应一个UITouch对象。这个对象里面保存着这次触摸的信息,比如触摸的位置,时间,阶段等,当手指移动时,系统会更新同一个UITouch对象。使其能一直保存该手指所在的触摸位置信息。当手指离开屏幕时,系统会销毁对应的UITouch对象。



@interfaceUITouch:NSObject@property(nonatomic,readonly)NSTimeIntervaltimestamp;@property(nonatomic,readonly)UITouchPhasephase;@property(nonatomic,readonly)NSUIntegertapCount;//touchdownwithinacertainpointwithinacertainamountoftime//majorRadiusandmajorRadiusToleranceareinpoints//ThemajorRadiuswillbeaccurate+/-themajorRadiusTolerance@property(nonatomic,readonly)CGFloatmajorRadiusNS_AVAILABLE_IOS(8_0);@property(nonatomic,readonly)CGFloatmajorRadiusToleranceNS_AVAILABLE_IOS(8_0);@property(nullable,nonatomic,readonly,strong)UIWindowwindow;@property(nullable,nonatomic,readonly,strong)UIViewview;@property(nullable,nonatomic,readonly,copy)NSArraygestureRecognizersNS_AVAILABLE_IOS(3_2);//获取当前位置-(CGPoint)locationInView:(nullableUIView)view;//获取上一个触摸点的位置-(CGPoint)previousLocationInView:(nullableUIView)view;//Forceofthetouch,where1.0representstheforceofanaveragetouch@property(nonatomic,readonly)CGFloatforceNS_AVAILABLE_IOS(9_0);//Maximumpossibleforcewiththisinputmechanism@property(nonatomic,readonly)CGFloatmaximumPossibleForceNS_AVAILABLE_IOS(9_0);@end





eg:让一个view随着手指的移动而移动



/当手指在view上移动时调用@paramtouches<#touchesdescription#>@paramevent<#eventdescription#>/-(void)touchesMoved:(NSSet)toucheswithEvent:(UIEvent)event{NSLog(@"%s",__func__);//获取UITouch对象UITouchtouch=[touchesanyObject];//获取当前点的位置CGPointcurP=[touchlocationInView:self];//获取上一个点的位置CGPointpreP=[touchpreviousLocationInView:self];//计算x的偏移量CGFloatoffsetX=curP.x-preP.x;//计算y的偏移量CGFloatoffsetY=curP.y=preP.y;//修改view的位置self.transform=CGAffineTransformTranslate(self.www.visa158.comtransform,offsetX,offsetY);}





就是根据UITouch对象中保存的位置信息来实现的。



事件的产生和传递:



当触摸事件产生后,系统会将该事件添加到一个由UIApplication管理的事件队列中去。UIApplication会从队列中取出最前面的事件,发送给应用程序的主窗口的处理。主窗口会在视图层次结构中,找一个最合适的视图并调用touches方法来处理触摸事件。触摸事件的传递是从父控件传递到子控件。如果父控件不能接收到触摸事件,那么子控件就不可能接收到触摸事件。



如何找到最合适的控件来处理事件?首先判断自己是否能接收触摸事件?触摸点是否在自己身上?从后往前遍历子控件,重复之前的两个步骤,如果没有符合条件的子控件,那么就自己最合适处理。



控件用hitTest:withEvent:方法来寻找最合适的view,用pointInside这个方法判断这个点在不在方法调用者即控件身上。



hitTest方法的底层实现:



-(UIView)hitTest:(CGPoint)pointwithEvent:(UIEvent)event{//判断当前控件是否能接收触摸事件if(self.userInteractionEnabled==NO||self.hidden==YES||self.alpha<=0.01){returnnil;}//判断触摸点是否在当前控件上if([selfpointInside:pointwithEvent:event]==NO){returnnil;}//从后往前遍历自己的子控件NSIntegercount=self.subviews.count;for(NSIntegeri=count-1;i>=0;i--){UIViewchildView=self.subviews[i];//把当前控件上的坐标系转换成子控件上的坐标系CGPointchildPoint=[selfconvertPoint:pointtoView:childView];//递归调用hitTest方法寻找最合适的viewUIViewfitView=[childViewhitTest:childPointwithEvent:event];if(fitView){returnfitView;}}//循环结束,没有比自己更合适的view,返回自己returnself;}





然而使用touches方法监听触摸事件是有缺点的,比如要自定义view,所以iOS3.2之后苹果推出了手势识别功能UIGestureRecognizer。UIGestureRecognizer是一个抽象类,它的子类才能处理具体的某个手势。



具体有以下几种手势:



//点按手势//UITapGestureRecognizertap=[UITapGestureRecognizeralloc]initWithTarget:<#(nullableid)#>action:<#(nullableSEL)#>//长按手势默认是触发两次//UILongPressGestureRecognizerlongP=[UILongPressGestureRecognizeralloc]initWithTarget:<#(nullableid)#>action:<#(nullableSEL)#>//轻扫手势默认方向是往右//UISwipeGestureRecognizerswipe=[UISwipeGestureRecognizeralloc]initWithTarget:<#(nullableid)#>action:<#(nullableSEL)#>//旋转手势//UIRotationGestureRecognizerrotation=[UIRotationGestureRecognizeralloc]initWithTarget:<#(nullableid)#>action:<#(nullableSEL)#>//捏合手势//UIPinchGestureRecognizerpinch=[UIPinchGestureRecognizeralloc]initWithTarget:<#(nullableid)#>action:<#(nullableSEL)#>//拖拽手势//UIPanGestureRecognizerpan=[UIPanGestureRecognizeralloc]initWithTarget:<#(nullableid)#>action:<#(nullableSEL)#>



实际运用:



@interfaceViewController()@property(weak,nonatomic)IBOutletUIImageViewimageView;@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];[selfsetUpPinch];[selfsetUpRotation];[selfsetUpPan];}#pragmamark-手势代理方法//是否允许开始触发手势//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer)gestureRecognizer//{//returnNO;//}//是否允许同时支持多个手势,默认是不支持多个手势//返回yes表示支持多个手势-(BOOL)gestureRecognizer:(UIGestureRecognizer)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer)otherGestureRecognizer{returnYES;}//是否允许接收手指的触摸点//-(BOOL)gestureRecognizer:(UIGestureRecognizer)gestureRecognizershouldReceiveTouch:(UITouch)touch{////获取当前的触摸点//CGPointcurP=[touchlocationInView:self.imageView];////if(curP.x




















献花(0)
+1
(本文系白狐一梦首藏)