在上一篇《iOS Programming –
触摸事件处理(1)》中了解触摸、事件和响应者之后,接下去学习如何处理用户的触摸事件。首先触摸的对象是视图,而视图的类UIView继承了UIRespnder类,但是要对事件作出处理,还需要重写UIResponder类中定义的事件处理函数。根据不通的触摸状态,程序会调用相应的处理函数,这些函数包括以下几个:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
当手指接触屏幕时,就会调用touchesBegan:withEvent方法;
当手指在屏幕上移时,动就会调用touchesMoved:withEvent方法;
当手指离开屏幕时,就会调用touchesEnded:withEvent方法;
当触摸被取消(比如触摸过程中被来电打断),就会调用touchesCancelled:withEvent方法。而这几个方法被调用时,正好对应了UITouch类中phase属性的4个枚举值。
上面的四个事件方法,在开发过程中并不要求全部实现,可以根据需要重写特定的方法。对于这4个方法,都有两个相同的参数:NSSet类型的touches和UIEvent类型的event。其中touches表示触摸产生的所有UITouch对象,而event表示特定的事件。因为UIEvent包含了整个触摸过程中所有的触摸对象,因此可以调用allTouches方法获取该事件内所有的触摸对象,也可以调用touchesForVIew:或者touchesForWindows:取出特定视图或者窗口上的触摸对象。在这几个事件中,都可以拿到触摸对象,然后根据其位置,状态,时间属性做逻辑处理。
-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event
{
UITouch*touch=[touchesanyObject];
if(touch.tapCount==2)
{
self.view.backgroundColor=[UIColorredColor];
}
}
上面的例子说明在触摸手指离开后,根据tapCount点击的次数来设置当前视图的背景色。不管时一个手指还是多个手指,轻击操作都会使每个触摸对象的tapCount加1,由于上面的例子不需要知道具体触摸对象的位置或时间等,因此可以直接调用touches的anyObject方法来获取任意一个触摸对象然后判断其tapCount的值即可。
检测tapCount可以放在touchesBegan也可以touchesEnded,不过一般后者跟准确,因为touchesEnded可以保证所有的手指都已经离开屏幕,这样就不会把轻击动作和按下拖动等动作混淆。
轻击操作很容易引起歧义,比如当用户点了一次之后,并不知道用户是想单击还是只是双击的一部分,或者点了两次之后并不知道用户是想双击还是继续点击。为了解决这个问题,一般可以使用“延迟调用”函数。
-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event
{
UITouch*touch=[touchesanyObject];
if(touch.tapCount==1)
{
[selfperformSelector:@selector(setBackground:)withObject:[UIColorblueColor]afterDelay:2];
self.view.backgroundColor=[UIColorredColor];
}
}
上面代码表示在第一次轻击之后,没有直接更改视图的背景属性,而是通过performSelector:withObject:afterDelay:方法设置2秒中后更改。
-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event
{
UITouch*touch=[touchesanyObject];
if(touch.tapCount==2)
{
[NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(setBackground:)object:[UIColorredColor]];
self.view.backgroundColor=[UIColorredColor];
}
}
双击就是两次单击的组合,因此在第一次点击的时候,设置背景色的方法已经启动,在检测到双击的时候先要把先前对应的方法取消掉,可以通过调用NSObject类的cancelPreviousPerformRequestWithTarget:selector:object方法取消指定对象的方法调用,然后调用双击对应的方法设置背景色为红色。
下面举个例子创建可以拖动的视图,这个主要通过触摸对象的位置坐标来实现。因此调用触摸对象的locationInView:方法即可。
CGPointoriginalLocation;
-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event
{
UITouch*touch=[touchesanyObject];
originalLocation=[touchlocationInView:self.view];
}
-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event
{
UITouch*touch=[touchesanyObject];
CGPointcurrentLocation=[touchlocationInView:self.view];
CGRectframe=self.view.frame;
frame.origin.x+=currentLocation.x-originalLocation.x;
frame.origin.y+=currentLocation.y-originalLocation.y;
self.view.frame=frame;
}
这里先在touchesBegan中通过[touch locationInView:self.view]获取手指触摸在当前视图上的位置,用CGPoint变量记录,然后在手指移动事件touchesMoved方法中获取触摸对象当前位置,并通过于与原始位置的差值计算出移动偏移量,再设置当前视图的位置。
分享到:
相关推荐
包含ios大部分触摸事件的执行流程,以及一些不常规的触摸事件处理方法
iOS Programming 6th big nerd ranch
Beginning ios programming with Swift xcode8 and ios10 1. Preface 2. GettingStartedwithXcode8Development 3. SwiftPlaygrounds 4. BuildYourFirstApp 5. HelloWorldAppExplained 6. IntroductiontoAutoLayout 7...
ios programming
iOS Programming The Big Nerd Ranch Guide 4th Edition.pdf 英文最新版,电子档 第一部分
IOS 11 Programming Fundamentals with Swift 4 amazon.com 上有售,值得分享!!
iOSProgramming4ed.zip书的最新版书中例子代码及素材(基于xcode5以及ios7)
iOS Programming: The Big Nerd Ranch Guide (6th Edition) (Big Nerd Ranch Guides) 6th Edition English | 1 Jan. 2017 | ISBN: 0134682335 | 416 Pages | AZW3/MOBI/EPUB/PDF (conv) | 44.19 MB by Christian ...
PDF版!!!no epub!iOS Programming The Big Nerd Ranch Guide pdf!
This book is for developers who have some experience with iOS programming, but want to take their skills to next level by unlocking the full potential of latest version of iOS with Swift to build ...
iOS 11 Programming Fundamentals with Swift Swift, Xcode, and Cocoa Basics 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
big nerd出的ios programmning第六版,从第四版开始入门的object-c,第五版变化大部分还是讲的object-c,第六版就开始讲swift了,很不错,跟着书写了一遍例子,基本上swift的语法掌握了
IOS7 Programming Cookbook
Mastering iOS 11 Programming(2nd)_Code 源码 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Once you master the fundamentals, you’ll be ready to tackle the details of iOS app development with author Matt Neuburg’s companion guide, Programming iOS 7—coming in December 2013., Explore the C...
iOS 6 Programming Cookbook中文版,前18章
Updated and expanded to cover iOS 7 and Xcode 5, iOS Programming: The Big Nerd Ranch Guide leads you through the essential concepts, tools, and techniques for developing iOS applications. After ...
iOS App Programming Guide
iOS 4 Programming Cookbook iphone ipad 需要ios4的编程书籍的朋友可以看看这个