2012-06-18 48 views
1

編輯:我有這樣的方法中一個的UIView放置在多個視圖控制器,其是即使我指定的動作的「數據源」的UIPageViewController雙抽頭選擇燒製多次

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self setBackgroundColor:[UIColor clearColor]]; 

     UITapGestureRecognizer *tapGR =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)]; 
     [tapGR setDelegate:self]; 
     [tapGR setNumberOfTapsRequired:1]; 
     [self addGestureRecognizer:tapGR]; 


     UITapGestureRecognizer *doubleTapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)]; 
     [doubleTapGR setNumberOfTapsRequired:2]; 
     [self addGestureRecognizer:doubleTapGR]; 

     [tapGR requireGestureRecognizerToFail :doubleTapGR]; 

     [tapGR release]; 
     [doubleTapGR release]; 
    } 
    return self; 
} 

-(void)handleTap:(UITapGestureRecognizer *)tapRecognizer{ 
    if (!(tapRecognizer.state == UIGestureRecognizerStatePossible)) { 
     [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:kTapOnCenterNotificationName object:nil]]; 
    } 
} 

-(void)handleDoubleTap:(UITapGestureRecognizer *)doubleTapRecognizer{ 

    CGPoint point = [doubleTapRecognizer locationInView:self]; 

    LSSharedVariables *sharedVariables = [LSSharedVariables sharedInstance]; 
    [sharedVariables setDoubleTapPoint:point]; 

    [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:kLongPressNotificationName object:nil]]; 

} 

的頂應僅在手勢識別器狀態等於UIGestureRecognizerStateEnded時執行,日誌會多次顯示。我只想在這個區塊執行一次操作,我應該怎麼做?

編輯:我想出了雙擊方法被調用多次是UIPageViewController的頁面。我無法理解的是爲什麼singleTapGestureRecognizer不一樣。

+0

你是積極的,你沒有把handleDoubleTap添加到多個對象?可能他們都報告發生了doubleTap,並且每次獨立調用該方法。搜索「handleDoubleTap」只是爲了驗證您沒有多次添加它。 –

+0

你是對的,多個頁面是發送通知的觀察者,即使它們不可見他們正在收聽。 – Lolloz89

+0

我會將該評論添加爲答案,以便我們可以關閉此問題。真高興你做到了! –

回答

0

這聽起來像你可能有多個對象偵聽觸摸和所有調用「handleDoubleTap」當它發生獨立。這可以解釋爲什麼當發生雙擊事件時你會看到它被多次調用。

我會先搜索您的代碼,確認您沒有多次添加它,因爲您遇到的行爲不是觸摸事件的標準行爲。

+0

其實有多個對象正在偵聽發送的NSNotification,而不是雙擊:)隨意更改您的答案.. – Lolloz89

1
- (void) handleDoubleTap : (UIGestureRecognizer*) sender 
{ 
NSLog (@"Double tap is being handled here"); 
} 

- (void) handleSingleTap : (UIGestureRecognizer*) sender 
{ 
NSLog (@"Single tap is being handled here"); 
} 

- (void) loadView 
{ 
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleDoubleTap]; 
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleSingleTap]; 

[singleTap requireGestureRecognizerToFail : doubleTap]; 

[doubleTap setDelaysTouchesBegan : YES]; 
[singleTap setDelaysTouchesBegan : YES]; 

[doubleTap setNumberOfTapsRequired : 2]; 
[singleTap setNumberOfTapsRequired : 1]; 

[self.view addGestureRecognizer : doubleTap]; 
[self.view addGestureRecognizer : singleTap]; 

[singleTap release]; 
[doubleTap release]; 
} 

試試這個

+0

嘿@ Lolloz89它現在工作嗎? – mshau

+0

這很有趣,因爲我已經有一個單擊手勢的不同行爲!無論如何,雙擊手勢被多次識別.. – Lolloz89