小编che*_*ear的帖子

在UITableViewCell中使用UIPanGestureRecognizer的问题

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

我已经尝试在我的UITableViewCell子类中实现它,但它似乎永远不会收到该事件.

这是我在UITableViewCell子类中放置的代码,用于尝试此功能.在我的init方法中

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

然后处理它的方法:

- (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 …
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview uigesturerecognizer ios

9
推荐指数
2
解决办法
9392
查看次数