2013-05-22 60 views
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,我知道手勢識別器正在工作,只要狀態而言預計...

我在做什麼錯在這裏?

回答

0

也可能有其他問題,但我可以告訴你,你的電話不會像你希望的那樣工作;你會最終傳遞的指針NSNumber對象,而不是一個int的,所以state將被設置爲某種垃圾的價值。創建一個方法,將執行self.state = UIGestureRecognizerStateRecognized並調用它。

+0

感謝的人......工作完美:) – Roshan