【问题标题】:Disable Mouse Click and Mouse Over in WKWebView在 WKWebView 中禁用鼠标单击和鼠标悬停
【发布时间】:2020-11-16 21:37:59
【问题描述】:

我是新人,请耐心等待。 MacOS(不是 iOS)上的 Xcode 使用 WKWebView / NSView /NSWindow 嵌入 youtube 视频:

@property (assign) IBOutlet NSView *webNSView;
@property (assign) IBOutlet WKWebView *webView;

- (void)windowDidLoad {
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.webNSView.frame];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:@"https://www.youtube.com/embed/C0DPdy98e4c"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.webNSView addSubview:webView]; }

如何禁用窗口中的所有鼠标交互,以便用户无法与 WKWebView 中的视频或初始化鼠标悬停交互。所以我希望禁用鼠标单击和窗口/视图上的鼠标悬停动作。我已经尝试过使用 youtube 的各种嵌入选项,但我认为需要在窗口/视图上完成。

【问题讨论】:

  • 只是挑剔 - 学习几件事:1) 拼写 XCode -> Xcode, MacOS -> macOS, ... 2) Markdown Editing Help - 内容viewDidLoad 没有缩进,... 3) 题名不需要包含 Xcode, macOS, ... 标签就够了 4) 不要过度使用xcode 之类的标签,阅读info page 学习当你应该使用它们时,......这些东西只是我在阅读了你的几个问题后的观察结果,它们确实显示了一个人是否关心(恕我直言)。
  • 感谢您的建议我对编码和堆栈非常陌生。

标签: objective-c xcode macos wkwebview nsview


【解决方案1】:

如果您真正想要的是禁用与窗口的所有交互,您可以将ignoresMouseEvent 属性设置为YES。您可以通过 NSWindowNSWindowController 子类来做到这一点:

@interface MyWindowController : NSWindowController
@end

@implementation MyWindowController

- (void)windowDidLoad {
    self.window.ignoresMouseEvents = YES;
}

@end

请注意,这确实会禁用所有鼠标与窗口的交互,包括标题栏。你通常想要一些不那么激进的东西。

另一种方法是继承WKWebView 并覆盖从NSResponder 类继承的鼠标事件处理方法。

@interface MyWebView : WKWebView
@end

@implementation MyWebView

- (void)mouseUp:(NSEvent *)event {}
- (void)mouseDown:(NSEvent *)event {}
- (void)mouseMoved:(NSEvent *)event {}
- (void)mouseEntered:(NSEvent *)event {}
- (void)mouseExited:(NSEvent *)event {}

@end

这些事件将被吞没,不会通过事件处理机制传播。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多