【问题标题】:ios How to make UITextView detect one tap?ios 如何让 UITextView 检测到一键点击?
【发布时间】:2011-11-25 15:29:31
【问题描述】:
- (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");
}

我的问题:

我想在UITextView 上点击一次时模拟两次点击,这是这段代码中的文本视图。但是当我在 textview 上点击一次或两次时,我不会从一次和两次点击中获得 NSLog,只是在它之外。我应该怎么做才能让它发挥作用?

【问题讨论】:

  • 首先,这些代码必须在自定义 UITextView 子类中才能工作,并且可能会干扰正常操作。其次,“我想在 UITextView 上点击一次时模拟两次点击”是什么意思,我的意思是什么目的?
  • @NJones 我想为用户提供一个选项,让他们可以选择一个或两个水龙头来实现某个功能。我想模拟两次点击以获取选定的文本,如果用户选择此选项,则只需点击一次即可。在 UITextView 上点击一次时是否可以强制点击两次? bensnider 的解决方案非常有效。但是如何模拟两次点击呢?

标签: ios uitextview uitouch tap


【解决方案1】:

我可能会在这里使用两个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
}

【讨论】:

  • 您的解决方案非常完美。但是,我真的想在点击一次时模拟两次点击以获取选定的文本。
  • 什么意思,获取选中的文本?
  • 下一个问题是,如何在允许 textview 检测链接上的点击的同时做到这一点?
【解决方案2】:

我遇到了一个类似的问题,我想在不编辑底层文本的情况下捕获对 textView 的点击。

此答案适用于 Swift 3.0。

对于我的解决方案,我实现了 textView 委托方法 textViewShouldBeginEditing 并为该值返回 false。这让我可以在没有任何额外开销的情况下捕获 textView 上的点击。

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

        return false
    }
}

请务必在您的 ViewController 类中指定 textView 以使用委托。

【讨论】:

  • 下次点击 textview 会触发吗?
【解决方案3】:

我遇到了同样的问题,其他答案对我不起作用。所以这就是我所做的。

我像另一个答案建议的那样附加了手势。然后我确保调用了选择的委托方法。我确实尝试过简单地选择单元格,但这并没有触发委托方法。我相信只有用户交互会导致调用委托方法,所以这段代码模仿了这种行为。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2018-02-04
    • 2013-10-20
    • 2013-09-13
    • 1970-01-01
    相关资源
    最近更新 更多