以编程方式启动UIGestureRecognizer

时间:2014-02-28 15:46:27

标签: ios

我想在将UIPanGestureRecognizer添加到屏幕截图后立即启动它。由于屏幕截图是通过代码创建的,因此当项目突出显示时,用户不会再次按下屏幕。那么......我如何以编程方式启动识别器?

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

1 个答案:

答案 0 :(得分:2)

您可以随时调用该方法。

例如,您添加了选择器

- (void) userCellDragged:(UIPanGestureRecognizer)sender;

对于你的平移手势识别器。

您可以通过添加

从视图中的任何位置调用此方法
[self userCellDragged:nil];

请记住向此方法添加一个参数,例如:

if (sender == nil) {
    // Triggered programmatically
}
else {
    // proceed as normal
}