触摸常见的事件有以下几种,触摸事件一般写在view文件中,因为viewController文件有可能控制不止一个view,不适合写触摸事件
// 开始触摸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"开始触摸touch");
} // 触摸结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"触摸结束touch");
} // 移动触摸
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"移动触摸touch"); // 1.获取触摸手势
UITouch *touch = [touches anyObject]; // 2.获取触摸的位置(获取一个触摸在某个视图上的位置)
CGPoint currentPoint = [touch locationInView:self.superview]; // 3.根据触摸的位置改变视图的位置
self.center = currentPoint; } // 触摸被打断
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"触摸被打断touch");
}