iOS——触摸事件与手势

本文详细介绍了iOS中触摸事件的处理方法,包括`touchesBegan`、`touchesMoved`、`touchesEnded`和`touchesCancelled`,并探讨了如何启用视图的交互以及多点触摸。此外,还解析了`UITouch`对象的属性和方法,如`window`、`view`、`tapCount`和`phase`。最后,文章讲解了手势识别器`UIGestureRecognizer`及其子类,如轻击、捏合、平移、轻扫、旋转和长按等手势的应用。
摘要由CSDN通过智能技术生成

1.触摸事件处理方法

响应者通过复写以下方法,可以监听触摸事件

//当一个或多个手指触碰屏幕时(开始)
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event;
//当一个或多个手指在屏幕上移动时(移动)
- (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event;
//当一个或多个手指离开屏幕时(结束)
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event;
//当触摸序列被诸如电话呼入这样的系统事件所取消时(取消)
(void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event;

注:人机交互(UIView 默认YES ; UIImageView,UILabel 默认关闭 NO)
//如果父视图交互关闭会影响到子视图
self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES; //多点触摸(默认NO)

2.UITouch触摸对象

当用户触摸视图时,调用视图如下方法
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
//获取触摸点个数
NSLog(@”开始触摸 : %ld”, touches.count);

//获取触摸事件的相关信息

UITouch *touch = [touches anyObject];
NSLog(@"touch is %@“,touch);
NSLog(@"阶段 : %ld", [touch phase]);
NSLog(@"widow : %@" ,[touch window]);

//(同一位置)获取连续触摸次数
NSInteger tapNumber = [touch tapCount];
NSLog(@"tapNumber is %ld”,(long)tapNumber);

// 获取触摸点所在的位置
CGPoint location = [touch locationInView:self.view];
NSLog(@"location : %@", NSStringFromCGPoint(location));

}

- UITouch 类中常用属性
window : 触摸产生时所处的窗口
view : 触摸产生时所处的视图
tapCount : 表示短时间内轻击屏幕的次数。因此可以根据 tapCount 判断单击、双击、或更多的轻击。
phase : 触摸事件在屏幕上有一个周期,即触摸开始、触摸点移动、触摸结束,还有中途取消。二通过phase可以查看当前触摸事件在一个周期中所处的状态。phase是UITouchPhase类型的,这是一个枚举配型,包含了
UITouchPhaseBegan ( 触摸开始)
UITouchPhaseMoved ( 接触点移动)
UITouchPhaseStationary ( 接触点无移动)
UITouchPhaseEnded ( 触摸结束)
UITouchPhaseCancelled ( 触摸取消)

UITouch 类中常用方法
函数返回一个CGPoint类型的值,表示触摸在view这个视图上的位置,这里返回的位置是针对view的坐标系的。
- (CGPoint)locationInView:(UIView *)view;
表示触摸在view这个视图上之前位置,这里返回的位置是针对view的坐标系的。
- (CGPoint)previousLocationInView:(UIView *)view;

- 手势识别器
UIGestureRecognizer
UIGestureRecognizer类,用于检测、识别用户使用设备时所用的手势。它是一个抽象类,定义了所有的手势的基本行为。以下是UIGestureRecognizer子类,用于处理具体的用户手势行为。
UITapGestureRecognizer (轻击)
UIPinchGestureRecognizer (捏合)
UIPanGestureRecognizer (平移)
UISwipeGestureRecognizer (轻扫)
UIRotationGestureRecognizer (旋转)
UILongPressGestureRecognizer (长按)

  • 一只手单击
 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleGesture)];
    [self.view addGestureRecognizer:singleTap];
  • 一只手双击
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleGesture)];
    doubleTap.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:doubleTap];
    //区别两种手势(一只手单击、双击)(即当双击时取消单击)
    [singleTap requireGestureRecognizerToFail:doubleTap];
  • 清扫手势
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeGesture];

    swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;//向上轻扫
    swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;//向下轻扫
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;//向左轻扫
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;//向右轻扫
  • 平移手势(滑动)
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:panGesture];
 - (void)pan:(UIPanGestureRecognizer *)_pan
{
    NSLog(@"平移");
    CGPoint point = [_pan locationInView:self.view];   //平移坐标
    NSLog(@"point is %f ,  %f",point.x , point.y);
}
  • 长安手势
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPressGesture.minimumPressDuration = 3;
    [self.view addGestureRecognizer:longPressGesture];
 - (void)longPress:(UILongPressGestureRecognizer *)_longPress
{
    NSLog(@"long press : %ld",[_longPress state]);
}
  • 旋转手势
  UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [self.view addGestureRecognizer:rotationGesture];
 - (void)rotation:(UIRotationGestureRecognizer *)_rotation
{
    float degress = _rotation.rotation * (180/M_PI);
    view.transform = CGAffineTransformRotate(view.transform, degress/1000);
}
  • 捏和手势
    static float scale = 0;
  • (void)pinch:(UIPinchGestureRecognizer *)_pinch
    {
    if (_pinch.state == UIGestureRecognizerStateEnded){
    return;
    }
    if (scale == 0){
    if (_pinch.scale > 0){
    NSLog(@”放大1”);
    }else{
    NSLog(@”缩小1”);
    }
    }else {
    if(scale - _pinch.scale < 0){
    NSLog(@”放大2”);
    }else {
    NSLog(@”缩小2”);
    }
    }
    scale = _pinch.scale;
    }
**手势代码**
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imgView.image = [UIImage imageNamed:@"feng.jpeg"];
    //人机交互   UIImage,UILable默认NO;UIView默认YES
    _imgView.userInteractionEnabled = YES;
    [self.view addSubview:_imgView];

    // 1. 点击手势

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    // 次数(单击、双击)
    singleTap.numberOfTapsRequired = 1;
    // 触摸数量(手指头的数量)
    singleTap.numberOfTouchesRequired = 1;
    [_imgView addGestureRecognizer:singleTap];

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    // 次数(单击、双击)
    doubleTap.numberOfTapsRequired = 2;
    // 触摸数量(手指头的数量)
    doubleTap.numberOfTouchesRequired = 1;
    [_imgView addGestureRecognizer:doubleTap];

    // 当双击发生时,单击失效
    [singleTap requireGestureRecognizerToFail:doubleTap];


    // 2. 长按手势

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [_imgView addGestureRecognizer:longPress];


    // 3. 拖动手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [_imgView addGestureRecognizer:pan];


    // 4. 旋转手势

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [_imgView addGestureRecognizer:rotation];


    // 5. 捏合手势

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [_imgView addGestureRecognizer:pinch];


    // 6. 轻扫手势
    //一个轻扫手势只能绑定一个方向,多个方向必须创建多个轻扫对象
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    // 指定轻扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    [_imgView addGestureRecognizer:swipe];

}

- (void)singleTap:(UITapGestureRecognizer *)sender
{
    NSLog(@"单击");
}

- (void)doubleTap:(UITapGestureRecognizer *)sender
{
    NSLog(@"双击");
}

- (void)longPress:(UILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始长按");
    } else if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"结束长按");
    }
}

- (void)pan:(UIPanGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateChanged) {
        NSLog(@"move...");
    } else if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"移动结束");
    }
}
- (void)rotation:(UIRotationGestureRecognizer *)sender
{    
    if (sender.state == UIGestureRecognizerStateChanged) {
        // 取得弧度
        CGFloat r = sender.rotation;
        NSLog(@"%f",r);

        // PI / 180角度 = rotation / X
        // 让视图跟着手势旋转
        [sender.view setTransform:CGAffineTransformMakeRotation(r)];        
    } else if (sender.state == UIGestureRecognizerStateEnded) {   
        [UIView animateWithDuration:0.4 animations:^{
            sender.view.transform = CGAffineTransformIdentity;
        }];        
    }
}

- (void)pinch:(UIPinchGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateChanged) {

        [sender.view setTransform:CGAffineTransformMakeScale(sender.scale, sender.scale)];
    } else if (sender.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.4 animations:^{
            sender.view.transform = CGAffineTransformIdentity;
        }];
    }
}

- (void)swipe:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"轻扫");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值