开发者

UIWebView Subclass with Custom HitTest

开发者 https://www.devze.com 2023-03-23 07:01 出处:网络
I have a UIWebView in many UITableViewCell objects which live in a UITableView. When you try to scroll the UITableView, sometimes the touches will get \"stuck\" on the UIWebView objects. However, I st

I have a UIWebView in many UITableViewCell objects which live in a UITableView. When you try to scroll the UITableView, sometimes the touches will get "stuck" on the UIWebView objects. However, I still need to be able to click links within the UIWebView objects. So, I created a subclass:

#import "SecretWebView.h"


@implementation SecretWebView

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([[event allTouches] count] == 0) {
        return self.superview;
    } else {
        return self; //I have tried many things here...
    }
}

@end

My hope was that I could return the superview (i.e. the TableViewCell) when there wasn't a link being clicked, and then return whatever would 开发者_开发问答normally be returned otherwise, so I can still click on links and get the same expected behavior. Everything works fine except for the "else" clause, where the UIWebView allows the link to be clicked.

In the code you'll see where I commented "I have tried many things here..." and that includes the following:

return [super hitTest:point withEvent:event];
return self;
return origObj; //where origObj was created in first line from the [super ...] call.

Please help me! How do I make it ignore any action but a single-tap on a link!?


Generally speaking, -[UIView hitTest:withEvent:] will look into its subview recursively, and might return itself or any of its subviews. In your case, UIWebview's hit is likely to be handled by one of its subview not itself. By invoking [super hitTest:withEvent:], you will get the right subview returned.

See this question for how hitTest:withEvent: works.

0

精彩评论

暂无评论...
验证码 换一张
取 消