点击事件,程序中都会用得到,平时我们用的比较多的有下面三种事件:
//下面三个touch事件用的比较多,大家也比较熟悉,这里就不多解释了
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//获取点击对象(如果对象只有一个,touch就是我们的点击对象)
UITouch *touch = [touches anyObject];
//点击次数
if (touch.tapCount == 1) {
NSLog(@"点击一次");
}
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//获取手指在屏幕上面的坐标
NSLog(@"x = %.2f , y = %.2f ",[touch locationInView:self.view].x , [touch locationInView:self.view].y);
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
还有一个 touchesCancelled的事件,我们用的不多,但是在一个成熟的APP当中也是非常重要的一种方法:
/*
下面这个方法我们一般用的比较少
touchesCancelled:在特殊情况下调用,终止点击事件,比如:有电话打进来的时候
*/
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
所以,当手机遇到特殊情况终止点击事件的时候,就会调用这个方法