触摸常见的事件有以下几种,触摸事件一般写在view文件中,因为viewController文件有可能控制不止一个view,不适合写触摸事件
1 // 开始触摸 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 3 NSLog(@"开始触摸touch"); 4 } 5 6 7 // 触摸结束 8 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 9 NSLog(@"触摸结束touch"); 10 } 11 12 13 // 移动触摸 14 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 15 NSLog(@"移动触摸touch"); 16 17 // 1.获取触摸手势 18 UITouch *touch = [touches anyObject]; 19 20 // 2.获取触摸的位置(获取一个触摸在某个视图上的位置) 21 CGPoint currentPoint = [touch locationInView:self.superview]; 22 23 // 3.根据触摸的位置改变视图的位置 24 self.center = currentPoint; 25 26 } 27 28 29 // 触摸被打断 30 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 31 NSLog(@"触摸被打断touch"); 32 }
原文:http://www.cnblogs.com/zhizunbao/p/5370906.html