Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios7 iOS 7 UITextView的本机链接不会取消触摸_Ios7_Uitextview_Uitextviewdelegate_Datadetectortypes - Fatal编程技术网

Ios7 iOS 7 UITextView的本机链接不会取消触摸

Ios7 iOS 7 UITextView的本机链接不会取消触摸,ios7,uitextview,uitextviewdelegate,datadetectortypes,Ios7,Uitextview,Uitextviewdelegate,Datadetectortypes,我想我在iOS 7的UITextView中遇到了一个bug,但我还没有找到任何关于它的参考或讨论,所以在这里尝试确认我没有遗漏什么: 虫子 我有一个UITextView设置,如下所示: UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, 280, 140)]; textView.editable = NO; textView.dataDetectorTypes = UIDataDetectorT

我想我在iOS 7的UITextView中遇到了一个bug,但我还没有找到任何关于它的参考或讨论,所以在这里尝试确认我没有遗漏什么:

虫子

我有一个UITextView设置,如下所示:

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, 280, 140)];
textView.editable = NO;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
textView.backgroundColor = [UIColor lightGrayColor];
textView.delegate = self;
textView.text = [NSString stringWithFormat:@"This is a UITextView in a UIViewController. It has editable %d, selectable %d, dataDectorTypes %d (UIDataDetectorTypeLink), and a link! http://www.google.com.\n\nIf you click the link, it works normally. If you press, then drag your finger away to cancel the touch, the highlight goes away but the action still fires. Shouldn't the action not fire?", textView.editable, textView.selectable, textView.dataDetectorTypes];
[self.view addSubview:textView];
textview文本包含一个链接,如果单击该链接,该链接将按预期工作:它将回调代理,代理将弹出一个警报:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"You clicked the link!" message:@"You clicked the link. Maybe you tried to move away to cancel the touch, but you clicked it anyway." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    return NO;
}
问题是,如果您按下链接,然后尝试拖动以取消触摸:链接的高亮状态消失,但代理会在您的手指离开屏幕时立即触发

我无法找到一种方法来告诉textview在应该取消的时候不要启动,或者检测它应该取消

我确信这是一个bug,因为视觉按下/取消状态和动作触发不匹配。

解决可怕的侵入性解决方法

我通过将UIApplication子类化来跟踪所有屏幕触摸来解决这个问题。通过按住最后一个触摸位置,我可以在代表中将该位置映射到链接,以确定该位置是否应被视为有效:

MyCustomApplication.m

在main.m中使用它

并绘制地图

@implementation MyCustomApplication

static CGPoint lastTouchVar;

- (void)sendEvent:(UIEvent *)event {
    if (event.type == UIEventTypeTouches) {
        lastTouchVar = [((UITouch*)event.allTouches.anyObject) locationInView:[[[UIApplication sharedApplication] delegate] window]];
    }
    [super sendEvent:event];
}

+ (CGPoint)lastTouch {
    return lastTouchVar;
}

@end
int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, @"MyCustomApplication", NSStringFromClass([MyAppDelegate class]));
    }
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    CGPoint lastTouchWRTTextView = [[[[UIApplication sharedApplication] delegate] window] convertPoint:[MyCustomApplication lastTouch] toView:textView];

    lastTouchWRTTextView.x -= textView.textContainerInset.left;
    lastTouchWRTTextView.y -= textView.textContainerInset.top;

    if (CGRectContainsPoint(CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height), lastTouchWRTTextView)) {

        int characterIndexForPoint = [textView.textContainer.layoutManager characterIndexForPoint:lastTouchWRTTextView inTextContainer:textView.textContainer fractionOfDistanceBetweenInsertionPoints:nil];
        if (NSLocationInRange(characterIndexForPoint, characterRange)) {
            // It's a real tap! Do something!
        }
    }
    return NO;
}