編輯:我有這樣的方法中一個的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不一樣。
你是積極的,你沒有把handleDoubleTap添加到多個對象?可能他們都報告發生了doubleTap,並且每次獨立調用該方法。搜索「handleDoubleTap」只是爲了驗證您沒有多次添加它。 –
你是對的,多個頁面是發送通知的觀察者,即使它們不可見他們正在收聽。 – Lolloz89
我會將該評論添加爲答案,以便我們可以關閉此問題。真高興你做到了! –