一、UITouch事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸");
//1、拿到触摸对象
UITouch * touch = [touches anyObject];
//2、拿到触摸的视图
UIView * view = touch.view;
//3、拿到触摸时间
NSTimeInterval time = touch.timestamp;
//4、触摸的状态 这是一个枚举类型
UITouchPhase phase = touch.phase;
//5、拿到上一次触摸的坐标
CGPoint point = [touch previousLocationInView:self.view];
//6、拿到当前触摸的坐标
CGPoint point1 = [touch locationInView:self.view];
}
//2.触摸移动的时候会调用
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸移动的时候会调用");
UITouch * touch = [touches anyObject];
//拿到上一次触摸的坐标
CGPoint point = [touch previousLocationInView:self.view];
//拿到当前触摸的坐标
CGPoint point1 = [touch locationInView:self.view];
//算出距离 上一次和当前坐标的距离
CGSize size = CGSizeMake(point1.x-point.x, point1.y-point.y);
//把偏移量加到 x y 值上。
self.myView.center = CGPointMake(self.myView.center.x+size.width, self.myView.center.y+size.height);
}
//3.触摸结束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸结束");
}
//4.触摸取消(比如来电的时候会取消触摸)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸取消");
}
参考文章: