如何在iOS 11中检测UIWebview drop?

时间:2021-07-18 05:25:57

I have a UIWebView. I am trying to implement ios11 drag and drop feature. When I drag item from Safari and drop it in my UIWebView, I want to know which delegate method will get called. Any suggestions?

我有一个UIWebView。我正在尝试实现ios11拖放功能。当我从Safari拖动项目并将其放入我的UIWebView时,我想知道将调用哪个委托方法。有什么建议么?

2 个解决方案

#1


3  

Most of the functionality of UIWebview is implemented in an undocumented class called UIWebBrowserView. It is that view which has its 'interactions' property set and the delegate for those interactions is set to that same UIWebBrowserView.

UIWebview的大多数功能都是在一个名为UIWebBrowserView的未记录的类中实现的。该视图具有其“interaction”属性集,并且这些交互的委托设置为相同的UIWebBrowserView。

Depending on your needs, there are 3 options to detect the drop:

根据您的需要,有3个选项可以检测掉落:

1) You could use javascript in the content of the web view to detect the drop. This would looks something like:

1)您可以在Web视图的内容中使用javascript来检测丢弃。这看起来像是这样的:

document.addEventListener('drop', function() {
     // Drop detected
} ) ;

2) You could disable the drop interactions in the UIWebBrowserView. That will allow them to propagate up into one of the containing views, and you can add your own interactions there. That would look like:

2)您可以在UIWebBrowserView中禁用拖放交互。这将允许它们传播到其中一个包含视图中,您可以在那里添加自己的交互。那看起来像是:

webView.scrollView.subviews[0].interactions = @[] ;  

Once you've done that, the you can create your own interactions and add them to the container view:

完成后,您可以创建自己的交互并将其添加到容器视图中:

 UIDropInteraction *dropInteration = [[UIDropInteraction alloc]initWithDelegate:myController] ;
 [webView addInteraction:dropInteration] ;
 webView.userInteractionEnabled = YES ;

You would then implement the UIDropInteractionDelegate functionality in 'myController'

然后,您将在'myController'中实现UIDropInteractionDelegate功能

3) You could swizzle UIWebBrowserView and replace the UIDropInteractionDelegate methods with your own.

3)你可以调用UIWebBrowserView并用你自己的方法替换UIDropInteractionDelegate方法。

#2


2  

UIDragInteractionDelegate & UIDropInteractionDelegate protocols are what you're looking for.

您正在寻找UIDragInteractionDelegate和UIDropInteractionDelegate协议。

Check this out as well: How to Drop Images in UIViewController with UIDropInteraction (Ep 1)

看看这个:如何使用UIDropInteraction在UIViewController中删除图像(第1部分)

#1


3  

Most of the functionality of UIWebview is implemented in an undocumented class called UIWebBrowserView. It is that view which has its 'interactions' property set and the delegate for those interactions is set to that same UIWebBrowserView.

UIWebview的大多数功能都是在一个名为UIWebBrowserView的未记录的类中实现的。该视图具有其“interaction”属性集,并且这些交互的委托设置为相同的UIWebBrowserView。

Depending on your needs, there are 3 options to detect the drop:

根据您的需要,有3个选项可以检测掉落:

1) You could use javascript in the content of the web view to detect the drop. This would looks something like:

1)您可以在Web视图的内容中使用javascript来检测丢弃。这看起来像是这样的:

document.addEventListener('drop', function() {
     // Drop detected
} ) ;

2) You could disable the drop interactions in the UIWebBrowserView. That will allow them to propagate up into one of the containing views, and you can add your own interactions there. That would look like:

2)您可以在UIWebBrowserView中禁用拖放交互。这将允许它们传播到其中一个包含视图中,您可以在那里添加自己的交互。那看起来像是:

webView.scrollView.subviews[0].interactions = @[] ;  

Once you've done that, the you can create your own interactions and add them to the container view:

完成后,您可以创建自己的交互并将其添加到容器视图中:

 UIDropInteraction *dropInteration = [[UIDropInteraction alloc]initWithDelegate:myController] ;
 [webView addInteraction:dropInteration] ;
 webView.userInteractionEnabled = YES ;

You would then implement the UIDropInteractionDelegate functionality in 'myController'

然后,您将在'myController'中实现UIDropInteractionDelegate功能

3) You could swizzle UIWebBrowserView and replace the UIDropInteractionDelegate methods with your own.

3)你可以调用UIWebBrowserView并用你自己的方法替换UIDropInteractionDelegate方法。

#2


2  

UIDragInteractionDelegate & UIDropInteractionDelegate protocols are what you're looking for.

您正在寻找UIDragInteractionDelegate和UIDropInteractionDelegate协议。

Check this out as well: How to Drop Images in UIViewController with UIDropInteraction (Ep 1)

看看这个:如何使用UIDropInteraction在UIViewController中删除图像(第1部分)