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

时间:2012-03-20 14:16:04

标签: iphone uitextview uigesturerecognizer

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

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

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {    
    //fire my method here
}

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

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

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

感谢@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;
}

@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;
}

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

这对我有用。我只想控制当用户点击或长按链接时会发生什么

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    // check for long press event
    BOOL isLongPress = YES;
    for (UIGestureRecognizer *recognizer in self.commentTextView.gestureRecognizers) {
        if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
            if (recognizer.state == UIGestureRecognizerStateFailed) {
                isLongPress = NO;
            }
        }
    }

    if (isLongPress) {
        // user long pressed a link, do something
    } else {
        // user tapped on a link, do something
    }

    // return NO cause you dont want the normal behavior to occur
    return NO;
}

答案 2 :(得分:3)

改编Lucas Chwe对iOS 9(和8)工作的回答。在他的回答中看到评论。

要点:以“无需长按”开始反转逻辑,如果至少有一个UILongPressGestureRecognizer处于Began状态,则切换到“长按检测到”。

BOOL isLongPress = NO;
for (UIGestureRecognizer *recognizer in textView.gestureRecognizers) {
    if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        if (recognizer.state == UIGestureRecognizerStateBegan) {
            isLongPress = YES;
        }
    }
}

仍有点hackish,但现在为iOS 8和9工作。

答案 3 :(得分:2)

这有点棘手,但对我有用。

我在UITextView上添加了UIButton的子类,检查长触摸并以这种方式将它们传递给UITextView:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    isLongTouchDetected = NO;
    [self performSelector:@selector(longTouchDetected) withObject:nil afterDelay:1.0];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (isLongTouchDetected == YES) {
        [super touchesCancelled:touches withEvent:event];
    } else {
        [super touchesEnded:touches withEvent:event];
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(longTouchDetected) object:nil]; 
    }
}

- (void)longTouchDetected {
    isLongTouchDetected = YES;
    // pass long touch to UITextView
}

答案 4 :(得分:0)

您可以使用textViewDidChangeSelection协议的UITextViewDelegate方法吗?

答案 5 :(得分:0)

对于SWIFT [最简单的方式]

extension UITextField {

    override public func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        if action == "paste:" {
            return false
        }

        return super.canPerformAction(action, withSender: sender)
    }

    override public func addGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
        if gestureRecognizer.isKindOfClass(UILongPressGestureRecognizer) {
            gestureRecognizer.enabled = false
        }
        super.addGestureRecognizer(gestureRecognizer)
        return
    }
}

以上代码还可以在内容菜单中禁用“PASTE”选项。