一、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(@"觸摸取消");
}
參考文章: