如何使用 UISwipeGestureRecognizer 检测 UITableViewCell 中的滑动事件 [英] How to detect a swipe event in a UITableViewCell with a UISwipeGestureRecognizer

查看:68
本文介绍了如何使用 UISwipeGestureRecognizer 检测 UITableViewCell 中的滑动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableViewCell 类,我想在其中检测滑动事件(删除)以隐藏在 drawRect

I have a UITableViewCell class where I want to detect the swipe event (delete) in order to hide some graphics drawn in the drawRect

首先我在单元格中添加了一个 UISwipeGestureRecognice:

First I added a UISwipeGestureRecognice to the cell:

// Init swipe gesture recognizer
self.swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeCell:)];
self.swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
self.swipeRecognizer.delegate = self;
[self.contentView addGestureRecognizer:self.swipeRecognizer];

比我实现了一种对滑动事件做出反应的方法:

Than I implemented a method to react on the swipe event:

- (void)swipeCell:(UISwipeGestureRecognizer *)recognizer
{
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            self.swipeStartPoint = [recognizer locationInView:self.backgroundView];
            BaseLogDebug(INFO, @"Swipe Began at %@", NSStringFromCGPoint(self.swipeStartPoint));
            break;
        case UIGestureRecognizerStateChanged: {
            CGPoint currentPoint = [recognizer locationInView:self.backgroundView];
            CGFloat deltaX = currentPoint.x - self.swipeStartPoint.x;
            BaseLogDebug(INFO, @"Swipe Moved %f", deltaX);
        }
            break;
        case UIGestureRecognizerStateEnded:
            BaseLogDebug(INFO, @"Swipe Ended");
            break;
        case UIGestureRecognizerStateCancelled:
            BaseLogDebug(INFO, @"Swipe Cancelled");
            break;
        default:
            break;
    }
}

为了允许同时进行手势识别,我实现了以下方法:

In order to allow simultaneous gesture recognizer I implemented the following method:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
 shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

手势识别识别的唯一状态是状态 UIGestureRecognizerStateEnded.我的代码有什么问题?

The only state the gesture recognize recognizes is the state UIGestureRecognizerStateEnded. What's wrong with my code?

推荐答案

来自 UIGestureRecognizer 类参考文档:

离散手势识别器从 UIGestureRecognizerStatePossible 过渡到 UIGestureRecognizerStateFailed 或 UIGestureRecognizerStateRecognized.

手势识别器识别离散事件,例如轻击或滑动,但不会报告手势内的变化.换句话说,离散手势不会在 Began 和 Changed 状态之间转换,也不会失败或被取消.

UISwipeGestureRecognizer 是一个离散的手势.如果您想要连续(但相似)的手势,请改用 UIPanGestureRecognizer.

UISwipeGestureRecognizer is a discrete gesture. If you want a continuous (but similar) gesture, use a UIPanGestureRecognizer instead.

这篇关于如何使用 UISwipeGestureRecognizer 检测 UITableViewCell 中的滑动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆