gpt4 book ai didi

ios - Xamarin.forms 捏合和缩放

转载 作者:可可西里 更新时间:2023-11-01 05:53:37 25 4
gpt4 key购买 nike

有没有一种方法可以让我使用 ONLY Xamarin.Forms 控件进行捏合和缩放。

我想在 xamarin.forms(WebView 或图像或任何其他)的任何控件中显示图像,并能够从应用程序缩放。

最佳答案

截至本文发布时,还没有一种方法可以使用纯内置的 Forms 控件进行缩放。有一种方法可以实现这一点,但您必须为此实现原生渲染器。

我通过创建一个继承自 Xamarin.Forms.ContentView - PanGestureContainer 的类在我正在编写的应用程序中实现了这一点,该类具有触摸点最小/最大数量和要监听的事件等属性。

在 iOS 项目中,我为我的 View 制作了一个自定义渲染器,其中渲染器从 View 中获取属性并连接触摸事件监听器。

此外,我制作了一个可应用于其他 View 的可附加属性(又名行为),当应用它时,它从其父 View 中获取 View ,将其包装在 PanGestureRecognizer 中,另一个附加属性以相同的方式充当事件监听器包装器。

这是一个完整的 hack,但在 Xamarin 干净地实现它之前覆盖了缺失的功能


更新:现在有了示例代码,严重精简 因为发布太多,它应该让您了解如何实现这一点,而不是复制/粘贴解决方案。如果它看起来可能太多了,我相信有更好的方法,但它会起到作用,直到这个功能被内置。

  public abstract class BaseInteractiveGestureRecognizer : BindableObject, IInteractiveGestureRecognizer
{
public static readonly BindableProperty CommandProperty = BindableProperty.Create<BaseInteractiveGestureRecognizer, ICommand> ((b) => b.Command, null, BindingMode.OneWay, null, null, null, null);

public ICommand Command {
get {
return (ICommand)base.GetValue (BaseInteractiveGestureRecognizer.CommandProperty);
}
set {
base.SetValue (BaseInteractiveGestureRecognizer.CommandProperty, value);
}
}

public object CommandParameter {get;set;} // make bindable as above

public GestureState State { get;set;} // make bindable as above

public View SourceView{ get; set; }

public void Send ()
{
if (Command != null && Command.CanExecute (this)) {
Command.Execute (this);
}
}
}

public class PanGesture : BaseInteractiveGestureRecognizer
{
public uint MinTouches { get;set; } // make bindable
public uint MaxTouches { get;set; } // make bindable
// add whatever other properties you need here - starting point, end point, touch count, current touch points etc.
}

然后在iOS项目中:

public abstract class BaseInteractiveGestureRenderer : BindableObject,IGestureCreator<UIView>
{
public abstract object Create (IInteractiveGestureRecognizer gesture, Element formsView, UIView nativeView);

public static GestureState FromUIGestureState (UIGestureRecognizerState state)
{
switch (state) {
case UIGestureRecognizerState.Possible:
return GestureState.Possible;
case UIGestureRecognizerState.Began:
return GestureState.Began;
case UIGestureRecognizerState.Changed:
return GestureState.Update;
case UIGestureRecognizerState.Ended:
return GestureState.Ended;
case UIGestureRecognizerState.Cancelled:
return GestureState.Cancelled;
case UIGestureRecognizerState.Failed:
return GestureState.Failed;
default:
return GestureState.Failed;
}
}
}


using StatementsHere;
[assembly: ExportGesture (typeof(PanGesture), typeof(PanGestureRenderer))]
namespace YourNamespaceHere.iOS
{
public class PanGestureRenderer : BaseInteractiveGestureRenderer
{
public PanGestureRenderer () : base ()
{
}

#region IGestureCreator implementation

public override object Create (IInteractiveGestureRecognizer gesture, Element formsView, UIView nativeView)
{
PanGesture panGesture = gesture as PanGesture;
nativeView.UserInteractionEnabled = true;

UIPanGestureRecognizer panGestureRecognizer = null;
panGestureRecognizer = new UIPanGestureRecognizer (() => panGesture.Send());
}
}

关于ios - Xamarin.forms 捏合和缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25577255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com