UI - 事件处理UITouch

在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__);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值