0

我创建了一个UIGestureRecognizer子类LongPressGestureRecognizer来模拟长按手势。(是的,我知道已经存在的具体子类,我只是在学习objective-c并进行一些实验。)

我只覆盖了以下方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.state = UIGestureRecognizerStatePossible;
    [self performSelector:@selector(setState:) withObject:[NSNumber numberWithInt:UIGestureRecognizerStateRecognized] afterDelay:2];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    self.state = UIGestureRecognizerStateCancelled;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    self.state   = UIGestureRecognizerStateFailed;
}

在具有存储手势识别器属性的视图控制器中recognizer,我有以下代码:

- (LongPressGestureRecognizer *)recognizer
{
    if (!_recognizer) {
        _recognizer = [[LongPressGestureRecognizer alloc] init];
    }
    return _recognizer;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.recognizer addTarget:self action:@selector(log:)];

    [self.view addGestureRecognizer:self.recognizer];
}

- (IBAction)log:(LongPressGestureRecognizer *)recognizer
{
    //blah blah blah
}

我的问题是log:根本没有被调用...通过UIGestureRecogniserState在控制台中记录 s ,我知道手势识别器在状态方面按预期工作...

我在这里做错了什么?

4

1 回答 1

0

可能还有其他问题,但我可以告诉你,你的-performSelector:withObject:afterDelay:电话不会像你希望的那样工作;您最终将传递一个指向NSNumber对象而不是 的指针int,因此state将被设置为某种垃圾值。创建一个可以执行的方法self.state = UIGestureRecognizerStateRecognized并调用它。

于 2013-05-22T01:21:55.590 回答