标签:
事件响应与传递
UIResponder
1.首先要找到在链状结构中,最为适合处理事件相应的组件,如果该组件对事件进行到了处理,那么该事件传递(告一段路),如果最为适合相应该事件的组件没有重写(UIResponder方法),那么他会想回找第二适合处理用户相应事件的组件,如果在一个链状结构中没有任何一个组件处理用户的触控事件,那么该事件将被丢弃
UIApplication-Appdelgate->UIWindow->UIViewController->(UIView默认视图)->GPGreenView(UIResponder)
UIApplication-Appdelgate->UIWindow->UIViewController->(UIView默认视图)->//GPGreenView(UIResponder)->(redView)
一旦用户触摸到了屏幕,就会立即执行touchesBegan
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
-----------------------------
例 touchesBegan:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//UITouch 用户的手指,用户手指触控到屏幕,系统就会自动创建一个UITouch对象,并且在内部记录下来用户,触控的位置
//UITouch对象被存储在 touches 中
UITouch * touch = [touches anyObject];
//参考当前(自己)视图的坐标系,用户的触控位置应该在哪里
CGPoint currentPoint = [touch locationInView:self];
NSLog(@"point %@",NSStringFromCGPoint(currentPoint));
NSLog(@"%@ %@,%@",_name,NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}
-----------------------------
只要用户触控发生移动,就会立即调用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
-----------------------------
例 touchesMoved:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@ %@,%@",_name,NSStringFromClass([self class]),NSStringFromSelector(_cmd));
//touchesMoved 方法中
//1.可以获得手指当前坐标位置
//2.可以获得移动之前的一个位置
UITouch * touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
CGPoint prePoint = [touch previousLocationInView:self];
NSLog(@"currentpoint %@,prePoint %@",NSStringFromCGPoint(currentPoint),NSStringFromCGPoint(prePoint));
跟随操作
//1.手指在屏幕上移动的差值
//2.让Frame,x,y坐标增加差值范围
//3.更新Frame
CGRect rect = self.frame;
rect.origin.x += currentPoint.x - prePoint.x;
rect.origin.y += currentPoint.y - prePoint.y;
}
用户手指离开屏幕,就会立即触发
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
如果用户的操作,被系统功能打断的时候会触发,来电话
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
标签:
原文地址:http://www.cnblogs.com/CLiOS/p/5290711.html