IOS / Objective-C:使用视图中的UITapRecognizer检测点击大小

时间:2018-01-09 19:53:53

标签: ios objective-c uitapgesturerecognizer

被修改

我有一个带有UITapGestureRecognizer的textview,用于识别人们点击的单词。但是,UITextVIew本身会根据textView中加载的文本数量而改变大小。现在,textView无法识别文本视图中超出屏幕初始大小的 上的任何点击。

tap识别器当前在viewDidLoad中初始化,因为我只想创建一次。因此,在布局视图之前创建它。我应该将它移动到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;

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

我只用你的代码进行了小测试。 对我来说一切正常。我每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