ios - 以编程方式启动 UIGestureRecognizer

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

最佳答案

您始终可以单独调用该方法。

例如,您添加了选择器

- (void) userCellDragged:(UIPanGestureRecognizer)sender;

用于您的平移手势识别器。

您只需添加即可从 View 中的任何位置调用它

[self userCellDragged:nil];

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

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

关于ios - 以编程方式启动 UIGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22099606/

相关文章:

ios - 使用 Swift 将方向设置为纵向

ios - 我应该从 iOS 上的 Realm 中删除旧表吗?

android - 推送通知使用负载打开正确的页面

ios - 非标称类型 X 不支持显式初始化

iphone - 我会丢失带有新证书的钥匙串(keychain)吗?

ios - 使用 presentingViewController 关闭 viewController

objective-c - 从坐标中获取当前地名(在 nsstring 中)

ios - 如何在 Swift 中使用扩展协议(protocol)公开 Obj-C 类的现有属性

ios - 是否可以将 Metal API 与 OpenGL ES 3.0 同时用于图形计算?

objective-c - CS193p的作业1