我有一個長按手勢識別的視圖中它:UIMenuController不響應第一選擇,只有第二
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetected:)];
[self addGestureRecognizer:longPress];
[longPress release];
}
return self;
}
當檢測到長按,我想顯示UIMenuViewController以上述觀點來看在這一個動作,而當該菜單項被竊聽我想執行塊:
- (void)longPressDetected:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
[self becomeFirstResponder];
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *actionItem = [[UIMenuItem alloc] initWithTitle:@"Action" action:@selector(someActionSelector)];
[menuController setMenuItems:[NSArray arrayWithObject:actionItem]];
[actionItem release];
[menuController setTargetRect:self.frame inView:self.superview];
[menuController setMenuVisible:YES animated:YES];
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(copy:) || action == @selector(cut:) || action == @selector(delete:) ||
action == @selector(paste:) || action == @selector(select:) || action == @selector(selectAll:)) {
return NO;
}
else if (action == @selector(someActionSelector)) {
return YES;
}
else {
return [super canPerformAction:action withSender:sender];
}
}
- (void)someActionSelector {
if (self.actionBlock) {
self.actionBlock();
}
}
問題是,這只是第二長按,然後點按組合後的作品。我第一次長按視圖,我看到菜單,但點擊菜單什麼都不做。第二次再次看到菜單時,我點擊它,然後塊被執行。
調試器顯示someActionSelector
中的斷點只在第二次敲擊時到達。任何想法爲什麼這是?
您是否嘗試將'UIGestureRecognizerStateBegan'更改爲'UIGestureRecognizerStateRecognized'? – Costique 2012-03-14 19:12:22