Problems Using UIPanGestureRecognizer in UITableViewCell

I am trying to inject a UIPanGestureRecognizer into my UITableViewController to use to scroll to remove animations. Similar to the deletion used in the Clean up application, where, if you scroll the UITableViewCell left or right, the cell is moved and deleted.

I tried to implement this in my subclass of UITableViewCell, but it didn't seem to receive the event.

This is the code I injected into a subclass of UITableViewCell to try this function. In my init method

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

and then its processing methods:

 - (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;} ]; } } 

Cells do not move at all. Not quite sure what is going on here.

+4
source share
3 answers

If you do not want the syntax gluing gesture to be performed simultaneously with the scroll gesture in the form of a table, add a panorama gesture to your cell and make it a delegate:

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

And implement the following delegate method just to run if the panorama is horizontal:

 #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; } 

Swift3 ..

 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(ty) 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 } 
+6
source

In order for the gesture to be detected synchronously with scrollView panGesture, you need the following method:

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

Remember to set panGesture.delegate to your viewController. (Updated with OlivaresF comment.)

+12
source

Add a gesture recognizer to the content view.

 [self.contentView addGestureRecognizer:recognizer]; 
0
source

Source: https://habr.com/ru/post/1468818/


All Articles