2

I want to start the UIPanGestureRecognizer right after adding it to a screenshot. Because the screenshot is created through code, when an item has been highlighted, the user won't press on the screen again. So... How do I start the recognizer programmatically?

UIView *snapshot = [cell snapshotViewAfterScreenUpdates:NO];

    //use the cell to map the snapshot frame to the window because this does a perfect job of accounting for table offset, etc. Other methods put the view a little to the side or way off
    CGRect newFrame = snapshot.frame;
    newFrame.origin = [cell convertPoint:newFrame.origin toView:self.view.window];
    [snapshot setFrame:newFrame];

    [HelperMethods shadowForView:cell color:[UIColor blackColor] offset:CGSizeMake(1, 1) opacity:.7 radius:snapshot.frame.size.width/4];

    //[self.view addSubview:snapshot];
    newFrame.origin.y -=10;

    //move the frame a little to let user know it can be moved
    [UIView animateWithDuration:.2 animations:^{
        [snapshot setFrame:newFrame];
    }];

    //add a long press that kills the tap if recognized
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(userCellDragged:)];
    [pan setMinimumNumberOfTouches:1];
    [pan setMaximumNumberOfTouches:1];

    [cell addGestureRecognizer:pan];
Cœur
  • 37,241
  • 25
  • 195
  • 267
William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • 1
    By calling its target method? – Larme Feb 28 '14 at 15:48
  • So, what your saying is you want to trigger the "userCellDragged" method and you are trying to figure out how to pass the arguments that it would receive if it was actually triggered by the gesture recognizer? – ezekielDFM Feb 28 '14 at 16:00
  • Correct. Start the gesture through code – William Falcon Feb 28 '14 at 16:11
  • possible duplicate of [How can I send a UILongPressGesture programmatically?](http://stackoverflow.com/questions/14408455/how-can-i-send-a-uilongpressgesture-programmatically) – Srikanth Feb 28 '14 at 18:40
  • You may do something like this http://stackoverflow.com/questions/14408455/how-can-i-send-a-uilongpressgesture-programmatically – Srikanth Feb 28 '14 at 18:41

1 Answers1

2

You can always call the method on its own.

For example, you've added the selector

- (void) userCellDragged:(UIPanGestureRecognizer)sender;

For your pan gesture recognizer.

You could call this from anywhere in the view by simply adding

[self userCellDragged:nil];

Remember to add a parameter to this method something like:

if (sender == nil) {
    // Triggered programmatically
}
else {
    // proceed as normal
}
Logan
  • 52,262
  • 20
  • 99
  • 128