触摸事件与手势识别1

UIViewUIResponder的子类,可以覆盖下列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
注意:所有UIKit控件均继承自UIView

常用属性:

1.UITouch *touch = [touches anyObject];//1. 从NSSet中取出UITouch对象
2.CGPoint location = [touch locationInView:self.view]; 2. 手指触摸的位置
3.[touch previousLocationInView:]方法对位置进行修正
4.touches.count来判断是多点还是单点触摸
5. [self.view setMultipleTouchEnabled:YES]设置为yes 支持多点触摸
6.CGPoint greenPoint = [selfconvertPoint:point toView:self.greenView];//将point有点击的试图转到greenView上,返回此点在greenView上到坐标
7.[self.greenViewpointInside:greenPoint withEvent:event]判断greenPoint是否在greenView上
8.[touch view] 获取但前触摸到试图

#pragma mark 触摸移动 -在一次触摸事件中,可能会被调用多次
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1. 从NSSet中取出UITouch对象
    UITouch *touch = [touchesanyObject];
    // 2. 知道手指触摸的位置
    CGPoint location = [touchlocationInView:self.view];
    // 2.1 可以利用[touch previousLocationInView:]方法对位置进行修正
    CGPoint pLocation = [touchpreviousLocationInView:self.view];
    CGPoint deltaP =CGPointMake(location.x - pLocation.x, location.y - pLocation.y);
    CGPoint newCenter =CGPointMake(self.redView.center.x + deltaP.x, self.redView.center.y + deltaP.y);
    
    // 3. 设置红色视图的位置
    [self.redViewsetCenter:newCenter];
}



多点触摸:

iOS中,视图默认不支持多点触摸,通常在使用UITouch开发时,为了避免不必要的麻烦,也最好不要支持多点

需要支持多点必须把 [self.viewsetMultipleTouchEnabled:YES]设置为yes

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   //可以通过touches.count来判断是多点还是单点触摸
    // 遍历touches集合来添加图像
    NSInteger i =0;
    for (UITouch *touchin touches) {
        UIImageView *imageView = [[UIImageViewalloc]initWithImage:self.images[i]];
        CGPoint location = [touchlocationInView:self.view];//手指触摸的位置
        [imageView setCenter:location];
        [self.viewaddSubview:imageView];
        [UIViewanimateWithDuration:2.0fanimations:^{
            [imageView setAlpha:0.5f];
        } completion:^(BOOL finished) {
            [imageView removeFromSuperview];
        }];
        
        i++;
    }
}



摇晃事件:

摇晃的试图必须重写此方法,并返回yes

- (BOOL)canBecomeFirstResponder
{
    returnYES;
}
#pragma mark - 监听运动事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (UIEventSubtypeMotionShake == motion) {
        NSLog(@"摇晃啦");
        // 摇晃的后续处理
        //
        // 1. 记录用户当前位置(经纬度)
        // 2. 去服务器上查询当前摇晃手机的用户记录
        // 3. 根据用户所在经纬度,取出前N名距离用户最近的用户记录
        // 4. 将用户记录发送到用户手机
        // 5. 手机接收到数据后,self.tableView reloadData
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值