Vigor
Vigor

Reputation: 1754

UIImageView can't detect long press gesture in a customized swippable UITableViewCell which is a scrollview

I'm using the swippable UITableViewCell subclass as linked below:

https://github.com/CEWendel/SWTableViewCell

In my cell, there's a UIImageView. I added a UILongPressGestureRecognizer to the UIImageView. I want to make a 'Wiggle-Jiggle' effect when the UIImageView was long pressed. But every time I long pressed the UIImageView, the whole cell was selected. So I dived into the swippable UITableViewCell code. I found that the cell itself was also set a UILongPressGestureRecognizer as following:

    self.longPressGestureRecognizer = [[SWLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPressed:)];
    self.longPressGestureRecognizer.cancelsTouchesInView = NO;
    self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration;
    self.longPressGestureRecognizer.delegate = self;
    [self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];

The code is from SWTableViewCell.m which you can find from the above link.

Now I want to know is there a way to lock the UILongPressGestureRecognizer to the cell and trigger the action for the UIImageView when the UIImageView was long pressed?

Many thanks for all the help.

Upvotes: 1

Views: 849

Answers (2)

Vigor
Vigor

Reputation: 1754

Finally I found the reason. I posted it here just incase anybody else meet the same confusion in the future.

I the library I used, there is several lines code to set the longPressGestureRecognizer.

self.longPressGestureRecognizer = [[SWLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPressed:)];
    self.longPressGestureRecognizer.cancelsTouchesInView = NO;
    self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration;
    self.longPressGestureRecognizer.delegate = self;
    [self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];

There's one line: self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration; and the #define kLongPressMinimumDuration 0.16f. So the reason of my issue is the duration 0.16f is too small. It'll be triggered earlier than my long press. If I change the duration, like change it to 0.5s, everything is fine.

Upvotes: 1

hbk
hbk

Reputation: 11184

You can try to do next (think not the best solution, but you can try it)

Create custom cell for your tableView, add UIView as content holder. Than in cell.m file do next

  1. register your gestrude

    - (void)initGestrudeRecognizer{
        UILongPressGestureRecognizer *longGestrudeRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [self addGestureRecognizer:longGestrudeRecognizer];
    }
    

2.implement longPress action.In this method also all you need animation (you can easily modify it - below just as example)

- (IBAction)longPress:(id)sender{
    NSLog(@"long press gestrude recognized");
    [UIView animateWithDuration:0.05 animations:^{
        self.uiview.frame = CGRectMake(-20, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.05 animations:^{
                self.uiview.frame = CGRectMake(20, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
            } completion:^(BOOL finished){
               self.uiview.frame = CGRectMake(0, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
            }];
        }];
    }
  1. If you need to recognize more than one gestrude you need to implement <UIGestureRecognizerDelegate> protocol and add method that simple return YES

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

If you dont want that your cell become selectable - simple choose selection type of cell to NONE

enter image description here

As result I got something like thin (on longPress)

enter image description here

Also good article about swipeGestrude usage you can find HERE. It's was very helpfull for me.

Upvotes: 0

Related Questions