1、因为点击检测视图也是一个响应者对象,应用程序在处理触摸事件时也可以利用响应链。响应链由一系列的下一个响应者组成。响应者链处理原则1. 点击检测视图或者第一响应者传递事件或动作消息给它的视图控制器(如果它有的话);如果没有一个视图控制器,就传递给它的父视图。2. 如果一个视图或者它的视图控制器不能处理这个事件或动作消息,它将传递给该视图的父视图。3. 在这个视图层次中的每个后续的父视图遵循上述的模式,如果它不能处理这个事件或动作消息的话。4. 最顶层的视图如果不能处理这个事件或动作消息,就传递给UIWindow对象来处理。5. 如果UIWindow 对象不能处理,就传给单件应用程序对象UIApp
2、lication。如果应用程序对象也不能处理这个事件或动作消息,将抛弃它。触摸信息有时间和空间两方面,时间方面的信息称为阶段(phrase),表示触摸是否刚刚开始、是否正在移动或处于静止状态,以及何时结束也就是手指何时从屏幕抬起。触摸信息还包括当前在视图或窗口中的位置信息,以及之前的位置信息(如果有的话)。当一个手指接触屏幕时,触摸就和某个窗口或视图关联在一起,这个关联在事件的整个生命周期都会得到维护。触摸事件的阶段事件处理方法在给定的触摸阶段中,如果发生新的触摸动作或已有的触摸动作发生变化,应用程序就会发送这些消息:当一个或多个手指触碰屏幕时,发送touchesBegan:withEvent
3、:消息。当一个或多个手指在屏幕上移动时,发送touchesMoved:当一个或多个手指离开屏幕时,发送touchesEnded:当触摸序列被诸如电话呼入这样的系统事件所取消时,发送touchesCancelled:触摸事件实例 EventInfo#import interface TouchView : UIView - (void)logTouchInfo:(UITouch *)touch;endimplementation TouchView(UITouch *)touch CGPoint locInSelf = touch locationInView:self; CGPoint loc
4、InWin = touch locationInView:nil; NSLog( touch.locationInView = %2.3f, %2.3f, locInSelf.x, locInSelf.y); touch.locationInWin = %2.3f, %2.3f, locInWin.x, locInWin.y); touch.phase = %d, touch.phase); touch.tapCount = %d, touch.tapCount);- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
5、touchesBegan - touch count = %d, touches count); for(UITouch *touch in event.allTouches) self logTouchInfo:touch; touch.phase,触摸事件的阶段。touch.tapCount,触摸事件的轻碰次数,可以判断双击事件。UIEvent 的allTouches方法,可以获得触摸点的集合,可以判断多点触摸事件。- (void)touchesMoved:touchesMoved - touch count = %d- (void)touchesEnded:touchesEnded -
6、touch count = %d- (void)touchesCancelled:touchesCancelled - touch count = %d手势在iPhone中很重要,手势就是手触摸屏幕的方式。单碰击双碰击多点触摸(合拢和展开)轻抚 单碰击和双碰击实例:MultiTap单碰击为红色,双碰击为蓝色interface MultiTapView :#import MultiTapView.himplementation MultiTapView- (void)turnBlue self.backgroundColor = UIColor blueColor;- (void)turnRed
7、 self.backgroundColor = UIColor redColor;/START:code.MultiTapView.touchesBegan: UITouch *touch = touches anyObject; if(touch.tapCount = 2) self class cancelPreviousPerformRequestsWithTarget:self selector:selector(turnRed) object:/END:code.MultiTapView.touchesEnded: if(touch.tapCount = 1) self perfor
8、mSelector:selector(turnRed) withObject:nil afterDelay:0.10f; self turnBlue;self performSelector: 是在0.1秒后调用turnRed方法。self classcancelPreviousPerformRequestsWithTarget:self selector:selector(turnRed) object: 是取消调用方法turnRed。多点触摸(合拢和展开)PinchZoomPinchZoomView .h文件QuartzCore/QuartzCore.hinterface PinchZoo
9、mView : CALayer *robotLayer; CGFloat previousDistance; CGFloat zoomFactor; BOOL pinchZoom;property(nonatomic, retain) CALayer *robotLayer;m文件PinchZoomView.himplementation PinchZoomViewsynthesize robotLayer;- (void)awakeFromNib self.robotLayer = CALayer layer; UIImage *image = UIImage imageNamed:Robo
10、t.png; self.robotLayer.contents = (id)image CGImage; self.robotLayer.bounds = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); self.robotLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds); self.layer addSublayer:self.robotLayer; pinchZoom = NO; previousDi
11、stance = 0.0f; zoomFactor = 1.0f;awakeFromNib当nib文件被加载的时候,加载器会发送一个awakeFromNib的消息到nib文件中的每个对象,每个对象都可以定义自己的 awakeFromNib方法来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象是执行awakeFromNib 。robotLayer是 CALayer 对象,本例子中我们把图片对象添加到robotLayer对象中。使用 CALayer需要引入 头文件和添加QuartzCore.framework框架。code.PinchZoomView.touchesBega
12、n if(event.allTouches.count = 2) pinchZoom = YES; NSArray *touches = event.allTouches allObjects; CGPoint pointOne = touches objectAtIndex:0 locationInView: CGPoint pointTwo = touches objectAtIndex:1 locationInView: previousDistance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) + pow(pointOne.y - pointT
13、wo.y, 2.0f); else previousDistance 是获得两个点的距离。pow是平方函数。sqrt是开平方根函数。code.PinchZoomView.touchesMoved if(YES = pinchZoom & event.allTouches.count = 2) CGFloat distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) + zoomFactor += (distance - previousDistance) / previousDistance; zoomFactor = fabs(zoomFactor
14、); previousDistance = distance; self.robotLayer.transform = CATransform3DMakeScale(zoomFactor, zoomFactor, 1.0f);code.PinchZoomView.touchesEnded if(event.allTouches.count != 2) if(event.allTouches.count = 1) / NSArray *touches = event.allTouches allObjects;/ UITouch *touch = touches objectAtIndex:0; NSInteger tapCount = touch tapCount; if (tapCount = 2) zoomFactor += 0.4; else if (tapCount = 3) zoomFactor += 0.6; else if (tapCount = 4) zoomFactor += 0.8;- (void)dealloc self.robotLayer = nil; robotLayer release; super dealloc;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1