9

我正在尝试在我的 UITableViewController 中实现一个 UIPanGestureRecognizer 以用于滑动删除动画。类似于 Clear 应用程序中使用的滑动删除,如果您向左或向右滑动 UITableViewCell,则单元格会移动并被删除。

我已经尝试在我的 UITableViewCell 子类中实现它,但它似乎从未接收到该事件。

这是我放在 UITableViewCell 子类中以尝试此功能的代码。在我的初始化方法中

UIGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    recognizer.delegate = self;
    [self addGestureRecognizer:recognizer];

然后是处理它的方法:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:self.superview];
//might have to change view to tableView
//check for the horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y)) {
    return YES;
    NSLog(@"Panning");
}
return NO;
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
    //if the gesture has just started record the center location
    NSLog(@"handlePan");
    _originalCenter = self.center; //Declared as a CGPoint at the top of my TableViewCell
}

if (recognizer.state == UIGestureRecognizerStateChanged) {
    //translate the center (aka translate from the center of the cell)
    CGPoint translation = [recognizer translationInView:self];
    self.center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y);
    // determine whether the item has been dragged far enough to delete/complete

}

if (recognizer.state == UIGestureRecognizerStateEnded) {
    // the frame this cell would have had before being dragged
    CGRect originalFrame = CGRectMake(0, self.frame.origin.y, self.bounds.origin.x, self.bounds.size.height);
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = originalFrame;}
     ];
}
}

细胞根本不动。不太确定这里发生了什么

4

3 回答 3

20

如果您不希望单元格的滑动手势与表格视图滚动手势同时发生,则将平移手势添加到您的单元格并使其成为委托:

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

并实现以下委托方法以仅在平移水平时启动:

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // note: we might be called from an internal UITableViewCell long press gesture

    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {

        UIPanGestureRecognizer *panGestureRecognizer = (UIPanGestureRecognizer*)gestureRecognizer;
        UIView *cell = [panGestureRecognizer view];
        CGPoint translation = [panGestureRecognizer translationInView:[cell superview]];

        // Check for horizontal gesture
        if (fabs(translation.x) > fabs(translation.y))
        {
            return YES;
        }

    }

    return NO;
}

斯威夫特3..

override func awakeFromNib() {
    super.awakeFromNib()
    // do not use, say, layoutSubviews as layoutSubviews is called often
    let p = UIPanGestureRecognizer(target: self, action: #selector(yourPan))
    p.delegate = self
    contentView.addGestureRecognizer(p)
    }
}

override func gestureRecognizerShouldBegin(_ g: UIGestureRecognizer) -> Bool {
    if (g.isKind(of: UIPanGestureRecognizer.self)) {
        let t = (g as! UIPanGestureRecognizer).translation(in: contentView)
        let verticalness = abs(t.y)
        if (verticalness > 0) {
            print("ignore vertical motion in the pan ...")
            print("the event engine will >pass on the gesture< to the scroll view")
            return false
        }
    }
    return true
}
于 2016-09-14T15:48:11.893 回答
13

您需要以下方法才能检测到与滚动视图的 panGesture 同步的手势:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
   return YES; //otherGestureRecognizer is your custom pan gesture
}

请记住将 panGesture.delegate 设置为您的 viewController。(更新了 OlivaresF 的评论。)

于 2013-05-11T23:03:28.423 回答
0

将手势识别器添加到内容视图。

[self.contentView addGestureRecognizer:recognizer];
于 2013-03-12T21:24:21.600 回答