如何在不禁用上下文菜单的情况下拦截长按UITextView? [英] How to intercept long press on UITextView without disabling context menu?

查看:151
本文介绍了如何在不禁用上下文菜单的情况下拦截长按UITextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to intercept long press on UITextview, but don't want to disable the context menu option at the same time.

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

If I use gesture recognizer on textview, it will disable context menu so I am using the method like below now.

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

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

But, it only fires the method when context menu shows up after the user long press some words. So when the user long press a blank space, then only the magnifying glass shows up, I can't fire the method at the time.

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

Does anyone have better ideas? Thanks!

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

//////The Problem Solved//////

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

Thanks to @danh and @Beppe, I made it even with tap gesture on UITextView. I wanted to show the font bar on textview by long press.

@First,我将UITextview子类化。

@First, I subclassed the 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上实现的点击手势的冲突。

@I used the delay to solve the conflict with tap gesture I implemented on 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;
}


推荐答案

我通常会避免继承子类,除非docs明确表示,这对我有用。长按和上下文菜单。哎呀 - 答案只是由@Beppe加载。伟大的思想相似: - )

I usually avoid subclassing unless the docs explicitly suggest, this worked for me. Long press and context menu. Whoops - Answer just loaded by @Beppe. Great minds think alike :-)

@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

这篇关于如何在不禁用上下文菜单的情况下拦截长按UITextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆