一、事件
1、在iOS上,事件有多种形式
1)触摸事件 2)运动事件 3)远程控制事件
2、UIView不接收触摸事件的三种情况
1.不接收用户交互(交互是指的两个没有关系的东西之间的交互,比如触摸的一些方法,平时很少有人注意到交互,因为默认的是交互) userInteractionEnabled = NO(这种不交互式可以看得见的情况下,无法进行交互) 例如:self.view.userinteraction = NO; 假如你单独操作一个视图上的label,可以在外面设置他的交互(在其他视图上Label默认的是不可交互),也可以给它单独设置一个类,继承于UILabel ,进行重写initWithFrame来设置 例如 -(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.userInteractionEnabled = YES;支持交互 self.multipleTouchEnabled = YES;//支持多触点触摸 } return self; } 2.隐藏 hidden = YES(看不见的不交互) 3.透明(看不见,但是存在的交互) alpha = 0.0 ~ 0.01 提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的
二、事件处理基本方法:基本的触摸方法(这里面只是一些触摸的方法,具体的用法还是要和其他的用法结合在一起)
1、一个或多个手指触碰屏幕 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 2、一个或多个手指在屏幕上移动 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 3、一个或多个手指离开屏幕 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 4、触摸序列被诸如电话呼入这样的系统事件取消 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
三、UITouch触摸对象
当用户触摸屏幕时,事件会被封装成一个event实例,包含了用户触摸的相关信息,event实例中包含着若干个UITouch实例,一个touch代表着用户的一个手指
1、UITouch常用属性
1)window 触摸产生时所处的窗口 2)view 触摸产生时所处的试图 3)tapCount tap(轻击)操作,和鼠标单独单击操作类似,"tapCount表示短时间内轻击屏幕的次数",因此可以根据tapCount判断单击、双击或更多的轻击 双击试图是时单击也会执行的解决方法 if (touch.tapCount == 1) { //延迟0.5秒执行 runLoop会计算这个时间 [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5]; }else{ //告诉runLoop取消调用某个延迟的方法 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil]; [self doubleTap]; } 4)timestamp 记录了触摸事件产生或变化时的时间,单位是秒 5)phase //触摸的生命周期 触摸事件在屏幕上有一个周期,即触摸开始、触摸点移动、触摸点结束,还有中途取消,通过phase可以查看当前触摸事件在一个周期中所处的状态,pase是一个枚举,包含: //触摸开始 UITouchPhaseBegan //接触点移动 UITouchPhaseMoved //接触点无移动 UITouchPhaseStationary //触摸结束 UITouchPhaseEnded //触摸取消 UITouchPhaseCancelled
2、UITouch常用方法
1)返回一个CGPoint类型的值,表示触摸在view这个视图上的位置,这里返回的位置是针对view的坐标系的 - (CGPoint)locationInView:(UIView *)view 2)该方法记录了前一个坐标值 - (CGPoint)previousLocationInView:(UIView *)view:
四.举三个例子来解释Touch上面的一些用法
1.首先是放大和缩小:label不可移动,原地单击放大,双击缩小(你可以设置一个label或者一个button或者其他)
此程序代码写在新建的UILabel里面
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; if (touch.tapCount == 1) { NSLog(@"单击"); [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5]; } else if (touch.tapCount == 2) { NSLog(@"双击"); //告诉runLoop取消调用某个延迟的方法 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil]; [self doubleTap];//调用双击的方法 } } - (void)singleTap { //NSLog(@"单击等待"); self.transform = CGAffineTransformScale(self.transform, 0.5, 0.5); } - (void)doubleTap { // NSLog(@"双击取消单击"); self.transform = CGAffineTransformScale(self.transform, 2, 2); }
2.移动label的位置三种:拖动移动(move),刚点击开始移动和点击结束移动
(1)拖动移动(move): 在设定label的时候定义一个全局的UILabel对象
建立一个Label label = [[MyLabel alloc]initWithFrame:CGRectMake(100, 250, 200, 200)]; label.text = @"我是label"; label.userInteractionEnabled = YES; label.backgroundColor = [UIColor yellowColor]; [self.view addSubview:label]; -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //返回一个CGPoint类型的值,表示触摸在view这个视图上的位置,这里返回的位置是针对view的坐标系的 - (CGPoint) locationInView:(UIView *)view UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; label.center = point; }
(2)刚点击开始移动
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 上面移动的方法写在这里面即可 }
(3)点击结束移动
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 上面移动的方法写在这里面即可 }
3.模拟捏合(也就是双手缩放图片,这里用的是自定义的Label)
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (touches.count == 1) { NSLog(@"移动时调用"); UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; label.center = point; NSLog(@"%ld",touches.count);//查看几个手指触摸屏幕 } if (touches.count == 2) { NSArray *array = [touches allObjects];//求出所有的触摸点放到数组里 UITouch *touch1 = array[0]; UITouch *touch2 = array[1]; CGPoint point1 = [touch1 locationInView:self.view];//求point1出当前位置 CGPoint point2 = [touch2 locationInView:self.view];//求point2出当前位置 double distance = [self distance:point1 point:point2];//调用方法求出两个手指间的距离 NSLog(@"位置%f",distance); CGPoint point = label.frame.origin; // origin |ˈɒrɪdʒɪn 求 label 的x,y的坐标 起源 size只是大小设置 CGPoint center = label.center; label.frame = CGRectMake(point.x, point.y, distance, distance); label.center = center; NSLog(@"%lf",point.x); } } //求两个点之间的距离方法 - (double)distance:(CGPoint)p1 point:(CGPoint)p2 { // ((x1-x2)平方+(y1-y2)平方)开方 double distnce = sqrt(pow(p1.x-p2.x, 2)+pow(p1.y-p2.y, 2)); return distnce; }