首页 > 学院 > 开发设计 > 正文

touches,motion触摸事件响应

2019-11-14 18:08:37
字体:
来源:转载
供稿:网友
//触摸事件响应需要重写方法

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 }

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表