1.事件的概念:(UIEvent)是由硬件设备捕捉到用户对设备的操作,把这个操作抽象成一个事件对象
iOS
中的三大事件:触摸,晃动事件,远程控制事件;其中应用最广泛的是触摸事件
2.UIView是支持触摸的,由于UIView内部没有实现触摸相关的方法,所以我们在点击UIView创建的视图对象时,没有任何反应
3.如果想让一个视图对象触摸事件作出响应,需要在这个类的.m文件中实现根触摸相关一些方法
#pragma mark 1.刚开始的触摸的时候触发
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//当触摸到视图的时候改变自身的颜色
self.backgroundColor = [UIColor randomColor];
NSLog(@"%s",__FUNCTION__);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//当触摸到视图的时候改变自身的颜色
self.backgroundColor = [UIColor randomColor];
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 2.当手指在视图范围内移动的时候触发
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
self.backgroundColor = [UIColor randomColor];
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
self.backgroundColor = [UIColor randomColor];
self.center = CGPointMake(arc4random_uniform(220-100+1)+100, arc4random_uniform(460-100+1)+100);
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 3.当我们的手指离开视图的时候触发
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// tocuchs集合中存放的是我们触摸时,屏幕上的手指的对象
// 获取屏幕上的手指对象,由于屏幕上只有一根手指,所有集合中只存在一个手指对象
UITouch *finger = [touches anyObject];
//获取手指对象finger点击次数
// finger.tapCount
//点击一次的时候改变自身背景颜色
if (1==finger.tapCount) {
// self.backgroundColor =[UIColor randomColor];
//当点击次数是1的时候,延迟改变自身的颜色,看看是否还有一次以上的点击操作
[self performSelector:@selector(ChangeBackgroundColor) withObject:nil afterDelay:0.3];
}else if(2 == finger.tapCount){
//当点击次数大一1的时候,取消之前改变自身颜色的任务
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(ChangeBackgroundColor) object:nil];
//其他情况改变父视图颜色
self.superview.backgroundColor = [UIColor randomColor];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// tocuchs集合中存放的是我们触摸时,屏幕上的手指的对象
// 获取屏幕上的手指对象,由于屏幕上只有一根手指,所有集合中只存在一个手指对象
UITouch *finger = [touches anyObject];
//获取手指对象finger点击次数
// finger.tapCount
//点击一次的时候改变自身背景颜色
if (1==finger.tapCount) {
// self.backgroundColor =[UIColor randomColor];
//当点击次数是1的时候,延迟改变自身的颜色,看看是否还有一次以上的点击操作
[self performSelector:@selector(ChangeBackgroundColor) withObject:nil afterDelay:0.3];
}else if(2 == finger.tapCount){
//当点击次数大一1的时候,取消之前改变自身颜色的任务
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(ChangeBackgroundColor) object:nil];
//其他情况改变父视图颜色
self.superview.backgroundColor = [UIColor randomColor];
}
// UITouch存放的是手指对象
NSLog(@"%s",__FUNCTION__);
}
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 4.当触摸被中断的时候触发(eg:来电话了,来短信了)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s",__FUNCTION__);
}
- (void)ChangeBackgroundColor
{
self.backgroundColor =[UIColor randomColor];
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s",__FUNCTION__);
}
- (void)ChangeBackgroundColor
{
self.backgroundColor =[UIColor randomColor];
}
MoveView类 实现移动
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.获取手指对象
UITouch *finger = [touches anyObject];
//2.获取手指当前的在视图上的位置
CGPoint currentLoction = [finger locationInView:self];
//3.获取手指之前在视图上的位置
CGPoint previousLoction = [finger previousLocationInView:self];
//4.计数手指在x轴和y轴上的增量
//x轴上的增量
CGFloat dx = currentLoction.x - previousLoction.x;
//y轴上的增量
CGFloat dy = currentLoction.y - previousLoction.y;
//获取中心点
CGPoint center = self.center;
self.center = CGPointMake(center.x + dx, center.y + dy);
}
{
//1.获取手指对象
UITouch *finger = [touches anyObject];
//2.获取手指当前的在视图上的位置
CGPoint currentLoction = [finger locationInView:self];
//3.获取手指之前在视图上的位置
CGPoint previousLoction = [finger previousLocationInView:self];
//4.计数手指在x轴和y轴上的增量
//x轴上的增量
CGFloat dx = currentLoction.x - previousLoction.x;
//y轴上的增量
CGFloat dy = currentLoction.y - previousLoction.y;
//获取中心点
CGPoint center = self.center;
self.center = CGPointMake(center.x + dx, center.y + dy);
}
//移动的过程
MoveView *moveView = [[MoveView alloc]initWithFrame:CGRectMake(110, 234, 100, 100)];
moveView.backgroundColor = [UIColor blueColor];
[self.view addSubview:moveView];
[moveView release];
MoveView *moveView = [[MoveView alloc]initWithFrame:CGRectMake(110, 234, 100, 100)];
moveView.backgroundColor = [UIColor blueColor];
[self.view addSubview:moveView];
[moveView release];
PinchView类 实现捏合
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
//ios支持多点触摸,只是默认是单点触摸
self.multipleTouchEnabled = YES;
}
return self;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//如果touchs这个集合中的手指对象个数为1,就直接返回这个touchsMoved方法
if (1 == touches.count) {
return;
}else{
//得到集合中所有的手指对象,并使用数组存储
NSArray *allTouches = [touches allObjects];
UITouch *touth1 = [allTouches firstObject];
UITouch *touth2 = [allTouches lastObject];
//1.求出两个手指对象当前的位置
CGPoint currentFirstPoint = [touth1 locationInView:self];
CGPoint currentSecondPoint = [touth2 locationInView:self];
//2.当前两个点的距离
CGFloat currentDistance = [self distanceFromFirstPoint:currentFirstPoint toSecondPoint:currentSecondPoint];
//3.求出之前两个手指的位置
CGPoint previousFirstPoint = [touth1 previousLocationInView:self];
CGPoint previousSecondPoint = [touth2 previousLocationInView:self];
//4.之前两个点直接的距离
CGFloat previousDistance = [self distanceFromFirstPoint:previousFirstPoint toSecondPoint:previousSecondPoint];
//5.获取捏合前后两个手指间接地比例
CGFloat rate = currentDistance/previousDistance;
//6.缩放视图,如果视图的大小发生变化时,而中心点的位置不发生变化,修改bounds就可以了
self.bounds = CGRectMake(0, 0, self.bounds.size.width*rate, self.bounds.size.height*rate);
}
}
//封装一个计算距离的法发
-(CGFloat)distanceFromFirstPoint:(CGPoint)firstPoint toSecondPoint:(CGPoint)secondPoint{
CGFloat dx = firstPoint.x - secondPoint.x;
CGFloat dy = firstPoint.y - secondPoint.y;
return sqrt(dx*dx + dy*dy);
}
{
if (self = [super initWithFrame:frame]) {
//ios支持多点触摸,只是默认是单点触摸
self.multipleTouchEnabled = YES;
}
return self;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//如果touchs这个集合中的手指对象个数为1,就直接返回这个touchsMoved方法
if (1 == touches.count) {
return;
}else{
//得到集合中所有的手指对象,并使用数组存储
NSArray *allTouches = [touches allObjects];
UITouch *touth1 = [allTouches firstObject];
UITouch *touth2 = [allTouches lastObject];
//1.求出两个手指对象当前的位置
CGPoint currentFirstPoint = [touth1 locationInView:self];
CGPoint currentSecondPoint = [touth2 locationInView:self];
//2.当前两个点的距离
CGFloat currentDistance = [self distanceFromFirstPoint:currentFirstPoint toSecondPoint:currentSecondPoint];
//3.求出之前两个手指的位置
CGPoint previousFirstPoint = [touth1 previousLocationInView:self];
CGPoint previousSecondPoint = [touth2 previousLocationInView:self];
//4.之前两个点直接的距离
CGFloat previousDistance = [self distanceFromFirstPoint:previousFirstPoint toSecondPoint:previousSecondPoint];
//5.获取捏合前后两个手指间接地比例
CGFloat rate = currentDistance/previousDistance;
//6.缩放视图,如果视图的大小发生变化时,而中心点的位置不发生变化,修改bounds就可以了
self.bounds = CGRectMake(0, 0, self.bounds.size.width*rate, self.bounds.size.height*rate);
}
}
//封装一个计算距离的法发
-(CGFloat)distanceFromFirstPoint:(CGPoint)firstPoint toSecondPoint:(CGPoint)secondPoint{
CGFloat dx = firstPoint.x - secondPoint.x;
CGFloat dy = firstPoint.y - secondPoint.y;
return sqrt(dx*dx + dy*dy);
}
//创建捏合视图对象
PinchView *pinchView = [[PinchView alloc]initWithFrame:CGRectMake(60, 184, 200, 200)];
pinchView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:pinchView];
PinchView *pinchView = [[PinchView alloc]initWithFrame:CGRectMake(60, 184, 200, 200)];
pinchView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:pinchView];
[pinchView release];
UIResponder响应者
1.UIResponder响应者类,继承自NSObject,它是一个响应者的基类,它提供了一些处理事件的方法
2.什么是响应者(响应者对象):第一要继承自Responder 第二能对ios中的事件(触摸,晃动,远程控制事件)作出相应的对象就叫做响应者
UILabel,UIImageView默认用户交互是关闭的
响应者链:(由大到小)确定事件作用的对象时:UIAppcation-->AppDelegate-->window-->视图控制器-->视图控制器上的子视图
响应事件:(由小到大)视图控制器上的子视图-->视图控制器-->window-->Appdelegate-->UIAppcation如果都不处理这个事件就会被丢弃
关闭用户的交互后,这条响应者连就断开了,事件只能找他的上一级去处理,如果上一级都不出理,此时事件就不了了之
yellowView.userInteractionEnabled =YES;