iOS基础:UITouch

一、UITouch事件

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    NSLog(@"触摸");
    
    //1、拿到触摸对象
    UITouch * touch = [touches anyObject];
    
    //2、拿到触摸的视图
    UIView * view = touch.view;
    
    //3、拿到触摸时间
    NSTimeInterval time = touch.timestamp;
    
    //4、触摸的状态  这是一个枚举类型
    UITouchPhase phase = touch.phase;
    
    //5、拿到上一次触摸的坐标
    CGPoint point = [touch previousLocationInView:self.view];
    
    //6、拿到当前触摸的坐标
    CGPoint point1 = [touch locationInView:self.view];
    
    
}


//2.触摸移动的时候会调用
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸移动的时候会调用");
    UITouch * touch = [touches anyObject];
    //拿到上一次触摸的坐标
    CGPoint point = [touch previousLocationInView:self.view];
    //拿到当前触摸的坐标
    CGPoint point1 = [touch locationInView:self.view];
    //算出距离  上一次和当前坐标的距离
    CGSize size = CGSizeMake(point1.x-point.x, point1.y-point.y);
    //把偏移量加到 x y 值上。
    self.myView.center = CGPointMake(self.myView.center.x+size.width, self.myView.center.y+size.height);
}


//3.触摸结束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    NSLog(@"触摸结束");
}

//4.触摸取消(比如来电的时候会取消触摸)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸取消");
    
}

参考文章:

1、iOS_UITouch 事件 - 简书

2、UITouch 触摸事件处理(实例) - Just Code - ITeye技术网站

3、locationInView - zhangkongzhongyun的专栏 - 博客频道 - CSDN.NET

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值