2011-11-25 68 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以使用委托。