在UIResponder父类中封装着4个事件处理方法
1>-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
2>- (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
3>- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
4>- (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event
因为我们无法获得UIView的.m文件,所以我们选择用UIView的子类来实现这几个方法
下面是代码:
// 触摸开始时执行的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%s----%d",__FUNCTION__,__LINE__);
}
// 触摸过程中执行的方法
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 在视图触摸过程中变换颜色(随机色)
// self.backgroundColor = [UIColor colorWithRed:arc4random()%10/9.0 green:arc4random()%10/9.0 blue:arc4random()%10/9.0 alpha:1];
/**
* 得到先前的那个点和现在的点的相对位置,移到时不以移动点和中心点重叠
*/
// 获得手指所在的位置(locationInView:)
// 1>方法调用者 -- UITouch的对象
UITouch *touch = [touches anyObject];
// 2>参数 -- self\self.superView\self.window
CGPoint point = [touch locationInView:self.window];
CGPoint point0 = [touch previousLocationInView:self.window];
CGPoint point1 = self.center;
CGPoint point2 = CGPointMake(point1.x + point.x - point0.x, point1.y + point.y - point0.y);
// 将视图的center设置为手指所在的位置
[self setCenter:point2];
// UITouch *touch = [touches anyObject];
// // 2>参数 -- self\self.superView\self.window
// CGPoint point = [touch previousLocationInView:self.window];
// // 将视图的center设置为手指所在的位置
// [self setCenter:point];
// NSLog(@"%s----%d",__FUNCTION__,__LINE__);
}
// 触摸结束时执行的方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.backgroundColor = [UIColor yellowColor];
// NSLog(@"%s----%d",__FUNCTION__,__LINE__);
}
// 触摸被中断时执行的方法(电话来了)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%s----%d",__FUNCTION__,__LINE__);
}