如何使用ScnViewGestures.Forms在Xamarin.Forms中使用TouchDown和TouchUp事件创建视图

hen*_*non 2 android ios xamarin xamarin.forms

ScnViewGestures.Forms是一个Nuget包,该包允许使用自定义触摸处理程序在页面中设置任何子视图。但是,通过在github 1上阅读其稀疏文档,很难弄清楚如何使用它。那么,如何在Xamarin.Forms中使用TouchDown和TouchUp事件处理程序创建自定义视图?

hen*_*non 5

这是一个工作代码示例。不要忘记安装nuget包ScnViewGestures.Forms并像这样初始化它:

iOS(在AppDelegate.cs中):

Xamarin.Forms.Forms.Init ();
ViewGesturesRenderer.Init();
Run Code Online (Sandbox Code Playgroud)

Android:

Xamarin.Forms.Forms.Init (this, bundle);
ViewGesturesRenderer.Init();
Run Code Online (Sandbox Code Playgroud)

WinPhone:

Xamarin.Forms.Forms.Init ();
ViewGesturesRenderer.Init();
Run Code Online (Sandbox Code Playgroud)

然后,您可以创建自定义视图,例如这个小按钮,该视图单独处理向下和向上事件,而Xamarin.Forms标准按钮则无法执行。

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using ScnViewGestures.Plugin.Forms;

namespace ViewGesturesExample {
    public partial class ButtonView : ContentView
    {
        public ButtonView ()
        {
            InitializeComponent ();
            Content = _gestures = new ViewGestures () {
                HorizontalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
                VerticalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
            };
            _gestures.Content=_button=new Label () {
                HorizontalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
                VerticalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
                XAlign = TextAlignment.Center,
                YAlign = TextAlignment.Center,
            };
            _gestures.TouchBegan += OnTouchDown;
            _gestures.TouchEnded += OnTouchUp;          
            // here you have more events like 
            //Tap;
            //SwipeLeft;
            //SwipeRight;
            //SwipeUp;
            //SwipeDown.
        }

            Label _button;
            ViewGestures _gestures;

            private void OnTouchDown(object sender, EventArgs args) {

            }

            private void OnTouchUp(object sender, EventArgs args) {

            }
        }

}
Run Code Online (Sandbox Code Playgroud)