UITouch 触摸

UITouch 触摸

触摸是系统的一些方法,发生触摸事件的时候会自动调用这些方法,分别是:点击的时候调用;在屏幕上移动的时候,不断调用;离开屏幕的时候调用;被打断的时候调用 (电话,锁屏,…. )

  • 1.点击的时候调用的方法- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event;

代码:


// 按住alt键,可以使用二个手指头的手势
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    UITouch *touch = [touches anyObject];
    // 点击屏幕的次数
    NSLog(@"%lu",touch.tapCount);
    if (touch.tapCount == 1) {
        // 延迟0.5秒,执行singleClick方法
        [self performSelector:@selector(singleClick) withObject:nil afterDelay:0.5];
        // [self singleClick];
    } else {
        // 先取消还没有执行的单击方法
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleClick) object:nil];
        [self doubleClick];
    }
}

// 单击
- (void)singleClick {
    _imageView.transform = CGAffineTransformScale(_imageView.transform, 1.1, 1.1);
    // 介绍一下alertController, iOS8 新增的控件
    // UIAlertControllerStyleActionSheet  actionSheet类型
    // UIAlertControllerStyleAlert        alert类型
    // 设置警告控制器的标题和信息,以及类型。
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"警告" message:@"饿了就要吃,困了就睡,你以为这是哪儿!" preferredStyle:UIAlertControllerStyleActionSheet];
    // 给警告控制器,添加动作(按钮标题,按钮的类型,按钮点击的回调的block)
    [ac addAction:[UIAlertAction actionWithTitle:@"回家" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"我被点了!!");
    }]];
    [ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点取消按钮了!!");
    }]];
    // 对于xxxController来说,一般都需要present出来。
    [self presentViewController:ac animated:YES completion:nil];
}

// 双击
- (void)doubleClick {
    _imageView.transform = CGAffineTransformScale(_imageView.transform, 0.9, 0.9);
}
  • 2.在屏幕上移动的时候,不断调用的方法 - (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event;

代码:


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // NSLog(@"%@",NSStringFromSelector(_cmd));
    // 集合,多少个元素 (表示有多少个touch对象。1个手指头就是1...)
    NSLog(@"%lu",touches.count);
    // UITouch *touch = [touches anyObject];
    if (touches.count > 1) {
        UITouch *touch1 = touches.allObjects[0];
        // UITouch *touch2 = touches.allObjects[1];
        // NSLog(@"%@, %@",NSStringFromCGPoint([touch1 locationInView:self.view]), NSStringFromCGPoint([touch2 locationInView:self.view]));
        // 当前,第一个手指头,相对于self.view的坐标
        // [touch1 locationInView:self.view]
        // 第一个手指头,相对于self.view的 前一个时刻的坐标
        // [touch1 previousLocationInView:self.view]
        // NSLog(@"%@, %@",NSStringFromCGPoint([touch1 previousLocationInView:self.view]), NSStringFromCGPoint([touch2 previousLocationInView:self.view]));
        // 一般我们不这样去做,只是一个随便的小Demo,真要判断用手势的控件
        CGPoint l1 = [touch1 locationInView:_imageView];
        CGPoint p1 = [touch1 previousLocationInView:_imageView];
        CGRect frame = _imageView.bounds;
        _imageView.bounds = CGRectMake(0, 0, frame.size.width-l1.x-p1.x, frame.size.height-l1.y-p1.y);
    }
}
  • 3.离开屏幕的时候调用的方法 - (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event;

代码:


// 离开屏幕的时候调用
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // NSLog(@"%@",NSStringFromSelector(_cmd));
}
  • 4.被打断的时候调用的方法 - (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event;

代码:


// 被打断的时候调用 (电话,锁屏,.... )
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    // NSLog(@"%@",NSStringFromSelector(_cmd));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值