IOS/objective-C : Detect Taps Using UITapRecognizer from View that Changes in Size

标签 ios objective-c uitapgesturerecognizer

已编辑

我有一个带有 UITapGestureRecognizer 的 TextView 来识别人们点击的单词。然而,UITextVIew 本身的大小会根据加载到 textView 中的文本数量而改变。目前,textView 无法识别 在 textview 中超过屏幕初始大小的文本上发生的任何点击。

点击识别器当前在 viewDidLoad 中初始化,因为我只想创建它一次。因此,它是在布局 View 之前创建的。我应该将它移动到 viewwillappear 还是在已知 textview 的大小后触发的其他一些方法?或者有没有办法让 tapRecognizer 知道 textview 现在更大了?

屏幕使用自动播放,所以这可能是问题的一部分。

//This is how the recognizer is created in viewdidload:
 UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc]initWithTarget:self
 action:@selector(handleTap:)];
    tapList.cancelsTouchesInView = NO;
    [self.myView addGestureRecognizer:tapRec];

//This is method that handles tap
- (void) handleTap:(UITapGestureRecognizer *)recognizer{
     UITextView *textView =  (UITextView *)recognizer.view;
    CGPoint location = [recognizer locationInView:textView];
     CGPoint position = CGPointMake(location.x, location.y);
      UITextPosition *tapPosition = [textView closestPositionToPoint:position];
      UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityLine inDirection:UITextLayoutDirectionRight];
    NSString *tappedLine = [textView textInRange:textRange];
    }

//This is where size of view is changed in response to quantity of text in viewwillappear

CGFloat fixedWidth = myView.frame.size.width;
    //MAXFLOAT IS THE maximum float system allows
    CGSize newSize = [myView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = myView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    myView.frame = newFrame;
 [self.myView sizeToFit];
    self.myView.editable = NO;
    self.myView.scrollEnabled=NO;

感谢您的任何建议。

最佳答案

我只用你的代码做了小测试。 对我来说一切正常。我添加了每 7 秒更改一次文本的计时器以进行测试。 更改大小后,我始终可以点击具有新大小的 textView 中的任何文本。 一些额外的代码可能有问题?

我的测试项目 xCode 9.2:https://ufile.io/ngo47

#import "ViewController.h"

@interface ViewController ()
{
    IBOutlet UITextView* myView;
    NSTimer* timer;
}

@end

@implementation ViewController

//This is method that handles tap
- (void) handleTap:(UITapGestureRecognizer *)recognizer{
    UITextView *textView =  (UITextView *)recognizer.view;
    CGPoint location = [recognizer locationInView:textView];
    CGPoint position = CGPointMake(location.x, location.y);
    UITextPosition *tapPosition = [textView closestPositionToPoint:position];
    UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityLine inDirection:UITextLayoutDirectionRight];
    NSString *tappedLine = [textView textInRange:textRange];

    NSLog(@"%@", tappedLine);
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self resizeView];
}

- (void)resizeView{
    //This is where size of view is changed in response to quantity of text in viewwillappear

    CGFloat fixedWidth = myView.frame.size.width;
    //MAXFLOAT IS THE maximum float system allows
    CGSize newSize = [myView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = myView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    myView.frame = newFrame;
    [self->myView sizeToFit];
    self->myView.editable = NO;
    self->myView.scrollEnabled=NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //This is how the recognizer is created in viewdidload:
    UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                            action:@selector(handleTap:)];
    //tapList.cancelsTouchesInView = NO;
    [self->myView addGestureRecognizer:tapRec];

    NSArray* someTexts = @[@"text1, text1, text1, text1, text1, text1, text1, text1, text1, text1",
                           @"text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2, text2",
                           @"text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, text3, ",
                           ];
    static NSInteger textIndex = 0;
    timer = [NSTimer scheduledTimerWithTimeInterval:7.0 repeats:YES block:^(NSTimer * timer){
        self->myView.text = someTexts[textIndex];
        [self resizeView];
        textIndex ++;
        if (textIndex >= someTexts.count) {
            textIndex = 0;
        }
    }];
}

@end

关于IOS/objective-C : Detect Taps Using UITapRecognizer from View that Changes in Size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48175725/

相关文章:

ios - 长按后 Swift UiTableViewCell 重置

ios - 将 JSON 解析为 Objective C 中的预定义类

ios - scrollview 在访问 View Controller 的最后一个组件时向上滚动

ios - UICollectionViewCell高度根据图像,处理单元格之间的间距

objective-c - 覆盖 NSArray 的哈希值

ios - 如何在不阻止 textView 触摸的情况下向所有 UITextView 添加一个 UIGestureRecognizer

ios - 如果点击 iOS 屏幕上的任何其他地方,是否会关闭菜单?

ios - Firebase Facebook 登录错误 : FIRAuthErrorDomain - Unsuccessful debug_token response from Facebook

ios - AST 反序列化问题和构建模块错误

ios - 滚动 UITableView 时图像内容出现偏移