ios - 以编程方式更改 UILongPressGestureRecognizer 的状态

标签 ios selector uigesturerecognizer

我正在寻找一种方法来强制更改 UILongPressGestureRecognizer 的状态至 UIGestureRecognizerStateEndedUILongPressGestureRecognizer 创建我自己的“最大持续时间”扩展(基本上是为了创建一种触摸并保持超时功能)。

下面是我的尝试:

- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
        if (sender.state == UIGestureRecognizerStateBegan)
        {
            NSLog(@"Hold Gesture Started");
            timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timesup:) userInfo:nil repeats:NO];
            //perform a function - i.e. start something
        }
        else if(sender.state == UIGestureRecognizerStateEnded)
        {
            NSLog(@"Hold Gesture Complete");
            [timer invalidate];
            timer = nil;
            //perform a function - i.e. stop something             
        }
}
-(void)timesup:(NSTimer *)timer {
    NSLog(@"Timer Up!");
    [longpress setState:UIGestureRecognizerStateEnded];
}

问题是[longpress setState:UIGestureRecognizerStateEnded];虽然在 xcode 中推荐,但不断抛出此错误:

No visible @interface for 'UILongPressGestureRecognizer' declares the selector 'setState:'

关于如何解决/规避此问题的任何想法?我试着在 SO 上寻找解决方案,但我什么也没找到。话虽这么说,如果这与已经回答的问题重复,我深表歉意 - 我只是没有看到它(我保证!)

最佳答案

您收到此错误是因为默认情况下,当您将 UIKit 导入您的实现文件时,它会导入 UIGestureRecognizer.h,该文件将其状态属性公开声明为只读。如果您希望能够设置此属性,则必须导入 UIGestureRecognizerSubclass.h,它将此属性重新声明为可读写。请注意,这意味着在 UIGestureRecognizer 的子类中使用,我不太了解它的底层工作原理,无法说这种用法是安全的。

#import <UIKit/UIGestureRecognizerSubclass.h>

关于ios - 以编程方式更改 UILongPressGestureRecognizer 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24874261/

相关文章:

ios - 如果播放之间> 1秒,AVAudioPlayer和AudioServices都会延迟声音

ios - 如何禁用 "gesture recognizer"(XMCircleGestureRecognizer)?

ios - 当应用程序回到前台时如何播放声音文件?

ios - 无法使用 swift 2 在 IOS9 上的 webview 中加载 http 授权网站 url

jquery - 是否可以将定义的变量用作 'data-content' 用于 jQuery 中的数据属性选择器?

css - 我怎样才能得到包括类名的选择器

iphone - 我真的必须重写哈希只是因为我重写了 isEqual : for my subclass?

ios - 如何快速检测谷歌地图的相机位置

objective-c - 无法识别的选择器发送到... NSArray (但我没有使用 NSArray )

ios - 在同一个 UIImageView 上处理/检测 UIPanGestureRecognisor 和 UISwipeGestureRecognisor