美文网首页
007-手势事件

007-手势事件

作者: Yasic | 来源:发表于2017-11-26 19:18 被阅读36次

手势事件

ios 手势事件主要有

Recognizer Event
UITapGestureRecognizer 轻拍手势
UISwipeGestureRecognizer 轻扫手势
UILongPressGestureRecognizer 长按手势
UIPanGestureRecognizer 平移手势
UIPinchGestureRecognizer 捏合(缩放)手势
UIRotationGestureRecognizer 旋转手势
UIScreenEdgePanGestureRecognizer 屏幕边缘平移

Tap事件

Tap 事件就是简单的点击事件。

首先放上一个 UIView 用于点击,放上一个 UILabel 作为点击事件的响应,当点击 View 时累计点击次数。要注意 UIView 默认用户交互是关闭的,需要打开才能响应手势事件。

    UIView *tapView = [[UILabel alloc] initWithFrame:CGRectMake(0, screenHeight/2 - 32, screenWidth, 64)];
    tapView.backgroundColor = [UIColor blueColor];
    tapView.userInteractionEnabled = YES;
    [self.view addSubview:tapView];
    
    _tapNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, screenHeight/2 + 32, screenWidth, 32)];
    _tapNumberLabel.textAlignment = NSTextAlignmentCenter;
    [_tapNumberLabel setText:@"tapNumber: 0"];
    [self.view addSubview:_tapNumberLabel];

然后为 label 添加点击事件

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent)];
    tapRecognizer.numberOfTapsRequired = 1; //轻拍次数
    tapRecognizer.numberOfTouchesRequired = 1;//手指个数
    [tapView addGestureRecognizer:tapRecognizer];

可以看到两个属性

  • numberOfTapsRequired 表示轻拍次数
  • numberOfTouchesRequired 表示轻拍时手指的个数

Swipe 事件

Swipe 事件就是滑动事件。

首先还是添加一个 UIView 作为滑动的对象,然后在 UILabel 上显示滑动方向。

    UIView *swipeView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight/2)];
    swipeView.backgroundColor = [UIColor blueColor];
    swipeView.userInteractionEnabled = YES;
    [self.view addSubview:swipeView];
    
    _alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, screenHeight/2 + 32, screenWidth, 32)];
    _alertLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_alertLabel];

然后添加 Swipe 事件,可以指定滑动方向和滑动手指个数

    UISwipeGestureRecognizer *leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeEvent:)];
    leftSwipeRecognizer.numberOfTouchesRequired = 1; //手指个数
    leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [swipeView addGestureRecognizer: leftSwipeRecognizer];
    
    UISwipeGestureRecognizer *upSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeEvent:)];
    upSwipeRecognizer.numberOfTouchesRequired = 1; //手指个数
    upSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [swipeView addGestureRecognizer: upSwipeRecognizer];

在响应事件里再根据方向执行不同操作

- (void)swipeEvent:(UISwipeGestureRecognizer *)sender
{
    if (sender.direction == UISwipeGestureRecognizerDirectionUp)
    {
        _alertLabel.text = @"up up";
    }
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        _alertLabel.text = @"left left";
    }
}

LongPress 事件

首先设置布局,与上文一样

    UIView *targetView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight/2)];
    targetView.backgroundColor = [UIColor blueColor];
    targetView.userInteractionEnabled = YES;
    [self.view addSubview:targetView];
    
    _alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, screenHeight/2 + 32, screenWidth, 32)];
    _alertLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_alertLabel];

然后添加事件

    UILongPressGestureRecognizer *longRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressEvent:)];
    longRecognizer.numberOfTouchesRequired = 1; //手指个数
    longRecognizer.numberOfTapsRequired = 1; // 点击次数
    longRecognizer.minimumPressDuration = 0.5; //最小长按时间
    longRecognizer.allowableMovement = 10; //允许移动的距离,但是一旦被识别为长按后,比如长按时间到达最小长按时间以后,则移动不再受到这一属性的限制
    [targetView addGestureRecognizer:longRecognizer];

最后定义响应事件

- (void)longPressEvent:(UILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"began");
        _alertLabel.text = @"began";
    }
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"end");
        _alertLabel.text = @"end";
    }
}

这里关于状态 state,是父类 UIGestureRecognizer 的一个枚举,定义如下

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
    
    UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
    
    UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
    
    // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
};

其他事件

其他几个事件的基本过程与上面几个类似,具体绑定手势过程如下

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panEvent:)];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panEvent:)];

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

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

相关文章

网友评论

      本文标题:007-手势事件

      本文链接:https://www.haomeiwen.com/subject/mdhlbxtx.html