你的位置:首页 > 操作系统

[操作系统]touches,motion触摸事件响应

//触摸事件响应需要重写方法 1 // 触摸时触发该方法(消息发送) 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 3   UITouch *touch = [touches anyObject]; 4   CGPoint point = [touch locationInView:self.rootView.touchView];//locationInView:得到当前点击下在指定视图中位置的坐标 5   NSLog(@"触摸开始:%.2f,%.2f",point.x,point.y); 6 } 7 //但触摸移动时触发该事件 8 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 9   10   UITouch *touch = [touches anyObject];11   CGPoint point1 = [touch locationInView:self.rootView.touchView];12   CGPoint point2 = [touch previousLocationInView:self.rootView.touchView];//previouslocationInView:得到当前点的前一个点在指定视图中位置的坐标13 }14 // 触摸结束时触发该方法(消息发送)15 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{16   NSLog(@"触摸结束");17 }18 //发生其他情况,如来电话或意外退出时,执行该方法19 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{20   21 }

//手机摇一摇实现要重写方法 1 //开始时调用 2 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ 3   NSLog(@"摇一摇开始"); 4 } 5 //结束时调用 6 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ 7   NSLog(@"摇一摇结束"); 8 } 9 //意外情况调用10 - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{11   NSLog(@"意外退出");12 }