- (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。我该怎么做才能让它发挥作用?
答案 0 :(得分:17)
我可能会在这里使用两个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
}
答案 1 :(得分: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
答案 2 :(得分:1)
我遇到了类似的问题,我希望在不编辑基础文本的情况下捕获textView上的点击。
这个答案适用于Swift 3.0。
对于我的解决方案,我实现了textView委托方法textViewShouldBeginEditing并为该值返回false。这允许我捕获textView上的点击而无需任何额外的开销。
extension ViewController: UITextViewDelegate {
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
// Do something
return false
}
}
请务必分配textView以在ViewController类中使用委托。