如何在不禁用上下文菜单的情况下拦截长按UITextView?

Non*_*oto 10 iphone uitextview uigesturerecognizer

我想在UITextview上拦截长按,但不想同时禁用上下文菜单选项.

如果我在textview上使用手势识别器,它将禁用上下文菜单,所以我现在使用下面的方法.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {    
    //fire my method here
}
Run Code Online (Sandbox Code Playgroud)

但是,它只会在用户长按某些单词后显示上下文菜单时触发该方法.所以当用户长按空格时,只显示放大镜,我当时无法触发方法.

有没有人有更好的想法?谢谢!

//////解决了问题//////

感谢@danh和@Beppe,我甚至在UITextView上使用了点击手势.我想通过长按在textview上显示字体栏.

@First,我将UITextview子类化.

@interface LisgoTextView : UITextView {
    BOOL        pressing_;

}

@property (nonatomic)         BOOL      pressing;

@end


@interface LisgoTextView (private)
    - (void)longPress:(UIEvent *)event;
@end


@implementation LisgoTextView

@synthesize pressing = pressing_;


//--------------------------------------------------------------//
#pragma mark -- Long Press Detection --
//--------------------------------------------------------------//

- (void)longPress:(UIEvent *)event {

    if (pressing_) {

        //post notification to show font edit bar
        NSNotification *fontEditBarNotification = [NSNotification notificationWithName:@"fontEditBarNotification" 
                                                                                object:nil userInfo:nil];
        [[NSNotificationCenter defaultCenter] postNotification:fontEditBarNotification];
    }    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self performSelector:@selector(longPress:) withObject:event afterDelay:0.7];
    pressing_ = YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];    
    pressing_ = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];    
    pressing_ = NO;
}
Run Code Online (Sandbox Code Playgroud)

@I使用延迟来解决我在UITextView上实现的轻击手势的冲突.

- (void)tapGestureOnTextView:(UITapGestureRecognizer *)sender {

    //cancel here if long press was fired first
    if (cancelTapGesture_) {
        return;
    }

    //don't fire show font bar 
    cancelShowFontBar_ = YES;
    [self performSelector:@selector(enableShowFontBar) withObject:nil afterDelay:1.0];

    //method here   
}


- (void)showFontEditBar {

    //cancel here if tap gesture was fired first
    if (cancelShowFontBar_) {
        return;
    }

    if (fontEditBarExists_ == NO) {

        //method here    

        //don't fire tap gesture
        cancelTapGesture_ = YES;
        [self performSelector:@selector(enableTapGesture) withObject:nil afterDelay:1.0];
    }
}

- (void)enableTapGesture {
    cancelTapGesture_ = NO;
}

- (void)enableShowFontBar {
    cancelShowFontBar_ = NO;
}
Run Code Online (Sandbox Code Playgroud)

dan*_*anh 8

除非文档明确提出,否则我通常会避免使用子类,这对我有用.长按和上下文菜单.哎呀 - 答案只是由@Beppe加载.英雄所见略同 :-)

@interface TextViewSubclass ()
@property(assign,nonatomic) BOOL pressing;
@end

@implementation TextViewSubclass
@synthesize pressing=_pressing;

- (void)longPress:(UIEvent *)event {
    NSLog(@"long press");
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    self.pressing = YES;
    [self performSelector:@selector(longPress:) withObject:event afterDelay:2.0];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    self.pressing = NO;
}
@end
Run Code Online (Sandbox Code Playgroud)