2011-11-25 26 views
10
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"touchesBegan"); 

    //test 
    UITouch *touch = [event allTouches] anyObject]; 
    if ([touch tapCount] == 2) { 
     NSLog (@"tapcount 2"); 
     [self.textview becomeFirstResponder]; 

    } 

    else if ([touch tapCount] == 1) { 
     NSLog (@"tapcount 1"); 
     [self.textview becomeFirstResponder]; 
     [self.view performSelector:@selector(select:)]; 


    } 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touchesBegan:touches withEvent:event]; 
    NSLog(@"touchesMoved"); 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"****touchesEnded"); 
    [self.nextResponder touchesEnded: touches withEvent:event]; 
    NSLog(@"****touchesEnded"); 
    [super touchesEnded:touches withEvent:event]; 
    NSLog(@"****touchesEnded"); 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touchesCancelled:touches withEvent:event]; 
    NSLog(@"touchesCancelled"); 
} 

我的問題:ios如何使UITextView檢測一個水龍頭?

我要上一個UITextView,這是該代碼的TextView一次敲擊時,模擬兩個水龍頭。但是,當我在textview上點擊一次或兩次時,我不會從一個和兩個水龍頭中獲得NSLog,只能在它之外。我應該怎麼做才能使它工作?

+0

首先,這段代碼必須在自定義的UITextView子類中工作,並且可能會干擾正常的操作。其次,你的意思是「我想在UITextView上敲擊一次時模擬兩次敲擊」,我的意思是爲了什麼目的? – NJones

+0

@NJones我想給用戶一個選項來選擇一個或兩個特定功能的水龍頭。我想要模擬兩個水龍頭來獲取選定的文本,並使其只有一個水龍頭用戶,如果他們選擇此選項。在UITextView上點擊一次時是否可以強制執行兩次敲擊? bensnider的解決方案完美地工作。但如何模擬兩個水龍頭? – wagashi

回答

16

也許我會在這裏使用兩個gesture recognizers

//...some stuff above here probably in you're controllers viewDidLoad 

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapRecognized:)]; 
singleTap.numberOfTapsRequired = 1; 
[someTextView addGestureRecognizer:singleTap]; 
[singleTap release]; 

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapRecognized:)]; 
doubleTap.numberOfTapsRequired = 2; 
[someTextView addGestureRecognizer:doubleTap]; 
[doubleTap release]; 

而選擇將只是這樣的:

- (void)singleTapRecognized:(UIGestureRecognizer *)gestureRecognizer { 
    NSLog(@"single tap"); 
    // ...etc 
} 

- (void)doubleTapRecognized:(UIGestureRecognizer *)gestureRecognizer { 
    NSLog(@"double tap"); 
    // ...etc 
} 
+0

您的解決方案非常完美。但是,爲了獲取選定的文本,我真的想在點擊一次時模擬兩次敲擊。 – wagashi

+0

你是什麼意思,得到選定的文本? – NJones

+0

接下來的問題是,如何做到這一點,同時允許textview檢測鏈接上的水龍頭? –

1

我有同樣的問題,其他的答案並沒有爲我工作。所以這就是我所做的。

我附加了提示的另一個答案的手勢。然後我確定調用了用於選擇的委託方法。我沒有嘗試簡單地選擇單元格,但沒有激發委託方法。我相信只有用戶交互會導致委託方法被調用,所以這段代碼模仿了這種行爲。

https://gist.github.com/brennanMKE/e89bf7a28d96812d6a22

@implementation TappableTextView 

- (instancetype)init { 
    self = [super init]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 

- (instancetype)initWithCoder:(NSCoder *)coder { 
    self = [super initWithCoder:coder]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 

- (instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 

- (void)setup { 
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapRecognized:)]; 
    singleTap.numberOfTapsRequired = 1; 
    [self addGestureRecognizer:singleTap]; 
} 

- (void)singleTapRecognized:(id)sender { 
    UIView *superview = self.superview; 

    UICollectionViewCell *cell = nil; 
    NSIndexPath *indexPath = nil; 

    while (superview) { 
     if ([superview isKindOfClass:[UICollectionViewCell class]]) { 
      cell = (UICollectionViewCell *)superview; 
     } 

     if ([superview isKindOfClass:[UICollectionView class]] && cell) { 
      UICollectionView *collectionView = (UICollectionView *)superview; 
      indexPath = [collectionView indexPathForCell:cell]; 
      NSAssert(collectionView.delegate, @"Delegate must be defined"); 
      NSAssert([collectionView.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)], @"Selection must be supported"); 
      if (indexPath && [collectionView.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) { 
       [collectionView.delegate collectionView:collectionView didSelectItemAtIndexPath:indexPath]; 
      } 

      return; 
     } 

     superview = superview.superview; 
    } 
} 

@end 
0

我遇到過類似的問題,我想捕捉的一個TextView水龍頭,而無需編輯基礎文本。

此答案適用於Swift 3.0。

對於我的解決方案,我實現了textView委託方法textViewShouldBeginEditing並返回值爲false。這使我可以捕獲textView上的點擊而不會產生額外的開銷。

extension ViewController: UITextViewDelegate { 
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { 
     // Do something 

     return false 
    } 
} 

只要確保在您的ViewController類中分配textView以使用委託。

相關問題