我们正在使用xamarin.forms构建一个应用程序,它有一个特定的页面显示一个We视图。我们为webview使用自定义呈现器来启用一些在xamarin.forms We视图中没有实现的特性。在IOS版本的应用程序中,这个webview是一个UIWebView控件。我需要的是一种在UIWebView控件上捕捉触摸开始、移动和结束事件的方法,并能够计算触摸移动的距离。此外,我需要能够取消在UIWebView中触发的touchmove事件,这样内容直到我想要滚动时才会滚动。
我已经在android版本中通过使用android webview的Touch均衡器实现了这一点。它完全可以做我想做的事情,触摸事件有一个“已处理”属性,可以用来避免事件在webview中触发。
我已经找到了在IOS中捕捉滚动事件的各种方法。最有希望的似乎是实现到UIWebView的滚动视图的gestureRecognizer。这样我就能捕捉到触觉的开始,移动和结束事件。然而,我在这个实施上有几个问题:
我目前附加手势识别器的方式:
var test = new MyRecognizer();
webView.ScrollView.AddGestureRecognizer(test);手势识别器:
public class MyRecognizer : UIGestureRecognizer
{
private nfloat _startY;
public MyRecognizer()
{
this.CancelsTouchesInView = true;
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
var location = (touches.AnyObject as UITouch).LocationInView(View);
_startY = location.Y;
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
var location = (touches.AnyObject as UITouch).LocationInView(View);
var intCurrentY = location.Y;
if(someCondition)
{
//cancel the event somehow to prevent the webview from scrolling
//a post mentioned this should do the trick, but it doesnt work:
this.Enabled = false;
this.Enabled = true;
}
}
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
}
}我在这里走得对吗?为什么事件停止开火?在某些情况下,我如何取消事件以防止webview滚动(我不想总是禁用webview上的滚动)
发布于 2017-09-01 08:10:57
我自己想出来的。至少有一部分。事件停止触发,因为在移动过程中,UIWebView的UIPanGestureRecognizer的默认ScrollView捕获了这个手势。这是我自己的识别器的代码,它在捕获事件方面非常有用,并且不会在一段时间后停止:
public class TopBottomBarScrollRecognizer : UIPanGestureRecognizer
{
private nfloat _startY;
private nfloat _startX;
private HybridWebView _webView;
private UIWebView _nativeWebView;
public TopBottomBarScrollRecognizer()
{
this.DelaysTouchesBegan = false;
this.DelaysTouchesEnded = false;
this.CancelsTouchesInView = false;
//make sure the recognizer can work together with other recognizers
this.ShouldRecognizeSimultaneously = (a, b) => true;
}
public TopBottomBarScrollRecognizer(HybridWebView webView, UIWebView nativeWebView) : this()
{
_webView = webView;
_nativeWebView = nativeWebView;
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
var location = (touches.AnyObject as UITouch).LocationInView(_nativeWebView);
_startY = location.Y;
_startX = location.X;
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
var location = (touches.AnyObject as UITouch).LocationInView(_nativeWebView);
var deltaY = (float)(double)(location.Y - _startY);
var deltaX = (float)(double)(location.X - _startX);
_webView.scrollOccurred(deltaX, deltaY);
}
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
_webView.scrollEndOccurred();
}
}以上解决了问题的第一部分。关于如何防止默认识别器暂时停止的问题已经不那么重要了,因为现在的行为似乎很好。
https://stackoverflow.com/questions/45954416
复制相似问题