ios 如何让 UITextView 检测一键点击?

发布于 2024-12-18 01:59:33 字数 1211 浏览 6 评论 0原文

- (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)上点击一次时模拟两次点击。但是,当我在 textview 上点击一次或两次时,我不会从一次或两次点击中获取 NSLog,仅在其外部。我应该怎么做才能让它发挥作用?

- (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");
}

MY QUESTION:

I want to simulate two taps when tapping once on a UITextView, which is textview in this code. But I do not get NSLog from one and two taps when I tap either once or twice on textview, only outside it. What should I do to make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是一片海 2024-12-25 01:59:33

也许我会使用两个 手势识别器 这里。

//...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
}

Probably I would use two gesture recognizers here.

//...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];

And the selectors would just be like:

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

- (void)doubleTapRecognized:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"double tap");
    // ...etc
}
空心空情空意 2024-12-25 01:59:33

我遇到了类似的问题,我想捕获 textView 上的点击而不编辑底层文本。

此答案适用于 Swift 3.0。

对于我的解决方案,我实现了 textView 委托方法 textViewShouldBeginEditing 并返回 false 值。这使我能够捕获 textView 上的点击,而无需任何额外的开销。

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

        return false
    }
}

只需确保指定 textView 来使用 ViewController 类中的委托即可。

I encountered a similar issue where I wanted to capture a tap on a textView without editing the underlying text.

This answer is for Swift 3.0.

For my solution, I implemented the textView delegate method textViewShouldBeginEditing and returned false for the value. This allows me to capture taps on the textView without any additional overhead.

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

        return false
    }
}

Just be sure to assign the textView to use the delegate in your ViewController class.

橘亓 2024-12-25 01:59:33

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

我按照另一个答案的建议附上了手势。然后我确保调用了选择的委托方法。我确实尝试简单地选择单元格,但这并没有触发委托方法。我相信只有用户交互才会导致调用委托方法,因此此代码模仿了该行为。

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

I had the same problem and the other answers did not work for me. So this is what I did.

I attached the gesture like another answer suggested. Then I made sure the delegate method for selection was called. I did try simply selecting the cell but that did not fire the delegate method. I believe that only user interactions cause the delegate method to be called, so this code mimics that behavior.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文