我想攔截UITextview上的長按,但不想同時禁用上下文菜單選項。如何在不禁用上下文菜單的情況下攔截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;
}
@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;
}
ahahah:D Banzai爲「偉大的思想」! – Beppe 2012-03-20 15:04:42
即使我仍在努力解決與在UITextview上實現的tapGesture衝突,這是我的問題的答案。非常感謝你。 – 2012-03-20 16:40:39
我在衝突中做了這個,所以在答案部分寫下了工作上的缺陷。 – 2012-03-21 07:27:58