触摸、事件、响应者链
来源:互联网 发布:linux route add 编辑:程序博客网 时间:2024/05/30 04:33
一、事件
UIEvent:事件,是由硬件捕捉的一个表示用户操作设备的对象。
分三类:触摸事件、晃动事件、远程控制事件
触摸事件:用户通过触摸设备屏幕操作对象、输入数据。支持多点
触摸,包含1个到多个触摸点
二、触摸的基本概念
UIView支持触摸事件(因为继承于UIResponder),而且支持多点触摸。
需要定义UIView子类,实现触摸相关的方法。
touches..began、touches..moved、touches…ended、touches..canceled
三、响应者链
由多个响应者对象组成的链。
UIResponder响应者类。
iOS中所有能响应事件(触摸、晃动、远程事件)的对象都是响应者。
系统定义了一个抽象的父类UIResponder来表示响应者。其子类都是响应者。
检测触碰视图
硬件检测到触摸操作,会将信息交给UIApplication,开始检测。
UIApplication -> window -> viewController -> view ->
检测所有子视图
最终确认触碰位置,完成响应者链的查询过程。
处理触碰视图
检测到响应者后,实现touchesBegan:withEvent:等方法,即处理事件。
如果响应者没有处理事件,事件会向下传递。如果没有响应者处理,则丢弃触摸事件。
事件处理的顺序与触摸检测查询相反。
触摸的子视图
-> view -> viewController -> window -> UIApplication
阻断响应者链
响应者链可以被打断。无法完成检测查询过程。
视图类的属性:userInteractionEnabled。关闭后能阻断查询过程。
如下程序,点击变换位置及颜色,踩黑白块的原理应该类似吧。。。
#import "RootViewController.h"#import "TouchView.h"@interface RootViewController ()@property(nonatomic , retain)TouchView *touch;@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; self.touch =[[TouchView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; _touch.backgroundColor =[UIColor yellowColor]; [self.view addSubview:_touch ]; [_touch release]; #warning mark ------响应者链 //UIlabel,UIImageView他们的userInteractionEnable默认是NO UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 100)]; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label]; //将用户交互打开 label.userInteractionEnabled = YES; UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom]; button.frame =CGRectMake(0, 0, 80, 80); button.backgroundColor =[UIColor cyanColor]; [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside]; [label addSubview:button]; [label release];}- (void)clickButton{ NSLog(@"我被点击了");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#import <UIKit/UIKit.h>#import "UIColor+randomColor.h"@interface TouchView : UIView@end#import "TouchView.h"#import "UIColor+randomColor.h" @interface TouchView () @property (nonatomic ,assign)CGPoint startPoint;@end@implementation TouchView- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"+++++touches:%@",touches); NSLog(@"-----touches:%@",event); NSLog(@"%s",__func__); self.backgroundColor=[UIColor randomColor]; _startPoint = [[touches anyObject]locationInView:self];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"%s",__func__); CGPoint currentPoint =[[touches anyObject]locationInView:self ]; CGFloat tx = currentPoint.x - _startPoint.x; CGFloat ty =currentPoint.y -_startPoint.y; //第一种平移方式(改变center的位置) CGPoint tempCenter =self.center ; tempCenter.x +=tx; tempCenter.y +=ty; self.center =tempCenter; self.center = CGPointMake(arc4random()%276 + 50, arc4random()%568 + 50); ////**************////第二种平移方式(使用transform) //self.transform =CGAffineTransformTranslate(self.transform, tx, ty);}//- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//{// NSLog(@"%s",__func__);// // CGFloat xMin = self.frame.size.width/2;// UIView * superView = self.superview;// CGFloat xMax = superView.frame.size.width - xMin;// // CGFloat yMin = self.frame.size.height/2;// CGFloat yMax =superView.frame.size.height - yMin;// // CGFloat x =arc4random()%(int)(xMax - xMin +1)+xMin;// CGFloat y =arc4random()%(int)(yMax - yMin +1)+yMin;// // self.center =CGPointMake(x , y);- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"%s",__func__);}@end#import "UIColor+randomColor.h"@implementation UIColor (randomColor)+ (UIColor *)randomColor{ return [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];}@end
- 触摸事件<响应者链>
- 触摸、事件、响应者链
- iOS开发 - 响应者链触摸事件
- iOS - 响应者链触摸事件
- 06 事件、触摸、响应者链
- iOS - 触摸事件 ,响应者
- IOS--UI--LessonTouchView 触摸事件 & 响应者链
- iOS开发 - 响应者链触摸事件(一)
- iOS开发 - 响应者链触摸事件(二)
- 触摸事件的传递 && 响应者链条
- 触摸事件传递与响应者链条
- iOS触摸事件/响应者链条
- ios触摸事件响应链个人总结
- 响应触摸事件
- 触摸事件响应
- 触摸事件分发响应
- imageView响应触摸事件
- UI_触摸、响应者链
- Ted2
- C++ 异步回调
- 常见错误之java.lang.IllegalStateException: The application’s PagerAdapter changed the adapter’s cont。。。
- Fagment 与 Fragment之间通信(交互)
- Log4j日志配置
- 触摸、事件、响应者链
- Onsql和MySQL的启停脚本
- LeetCode-Count Complete Tree Nodes
- 安卓环境下载地址
- javaWEB开发遇到的400(一)
- jstack 使用
- Log4j按级别输出到日志文件
- 翻转子串
- HDU 2018 母牛的故事(dp)