您的位置:首页 > 其它

touches,motion触摸事件响应

2015-10-15 22:37 274 查看
//触摸事件响应需要重写方法

1 // 触摸时触发该方法(消息发送)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.rootView.touchView];//locationInView:得到当前点击下在指定视图中位置的坐标
NSLog(@"触摸开始:%.2f,%.2f",point.x,point.y);
}
//但触摸移动时触发该事件
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];
CGPoint point1 = [touch locationInView:self.rootView.touchView];
CGPoint point2 = [touch previousLocationInView:self.rootView.touchView];//previouslocationInView:得到当前点的前一个点在指定视图中位置的坐标
}
// 触摸结束时触发该方法(消息发送)
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸结束");
}
//发生其他情况,如来电话或意外退出时,执行该方法
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

}


//手机摇一摇实现要重写方法

1 //开始时调用
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"摇一摇开始");
}
//结束时调用
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"摇一摇结束");
}
//意外情况调用
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"意外退出");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: