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

时间:2015-08-07 11:39:39

标签: android ios xamarin xamarin.forms

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

1 个答案:

答案 0 :(得分:5)

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

iOS(在AppDelegate.cs中):

Xamarin.Forms.Forms.Init ();
ViewGesturesRenderer.Init();

机器人:

Xamarin.Forms.Forms.Init (this, bundle);
ViewGesturesRenderer.Init();

WinPhone:

Xamarin.Forms.Forms.Init ();
ViewGesturesRenderer.Init();

然后你可以创建像这个小按钮一样的自定义视图,分别处理向下和向上事件,这是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) {

            }
        }

}