我想截取UITextview上的长按,但不想同时禁用上下文菜单选项。
如果我在textview上使用手势识别器,它将禁用上下文菜单,因此我现在使用下面的方法。
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
//fire my method here
}
但是,只有在用户长按某些单词后显示上下文菜单时,才触发该方法。因此,当用户长按空白处时,只有放大镜出现,我当时无法启动该方法。
有谁有更好的主意?谢谢!
//////已解决的问题//////
感谢@danh和@Beppe,我什至可以在UITextView上轻按手势。我想通过长按在textview上显示字体栏。
@首先,我将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;
}
@我使用延迟来解决我在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;
}
最佳答案
除非文档明确建议,否则我通常避免子类化,这对我有用。长按和上下文菜单。糟糕-答案由@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