gpt4 book ai didi

c# - Xamarin IOS 加载 View 表单手势事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:06:31 26 4
gpt4 key购买 nike

我正在 try catch PanGesture 事件,因此当用户用他/她的手指在 View 中的任意位置向右滑动时,例如,RightView 出现,当向左滑动时,LeftView 出现。

我正在使用下面的代码。它来自 Xamarin 文档。我修改了一下。我的“currView”是当前 View 的导出。当我用鼠标在模拟器上滑动时,事件被正确捕获,但是当(在中断指向之后)在

的大括号处单步执行时
    if (recognizer.State != (UIGestureRecognizerState.Cancelled | UIGestureRecognizerState.Failed
{

代码在它和这个之间循环。

     HandleDrag(gesture))

每个像素一次(我认为)。

我试图触发另一个 View 的加载,但没有找到放置它的位置。我在

    Console.WriteLine ("Here")

在:

    currView.AddGestureRecognizer (gesture);

但每次代码完成循环时,它都会忽略它并结束执行。如何加载下一个 View ?

注释行:

    // NEED TO LOAD ANOTHER VIEW HERE 

显示我想要加载另一个 View 的位置。

这是我的代码

namespace Example_Touch.Screens.iPhone.GestureRecognizers
{
using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

public partial class GestureRecognizers_iPhone : UIViewController
{
private RectangleF originalImageFrame = RectangleF.Empty;
// The IntPtr and initWithCoder constructors are required for items that need
// to be able to be created from a xib rather than from managed code

public GestureRecognizers_iPhone(IntPtr handle)
: base(handle)
{
Initialize();
}

[Export("initWithCoder:")]
public GestureRecognizers_iPhone(NSCoder coder)
: base(coder)
{
Initialize();
}

public GestureRecognizers_iPhone()
: base("GestureRecognizers_iPhone", null)
{
Initialize();
}

public override void ViewDidLoad()
{
base.ViewDidLoad();


WireUpDragGestureRecognizer();
}

protected void WireUpDragGestureRecognizer()
{
// create a new tap gesture
UIPanGestureRecognizer gesture = new UIPanGestureRecognizer();

// wire up the event handler (have to use a selector)
gesture.AddTarget(() => HandleDrag(gesture));
currView.AddGestureRecognizer (gesture);
}

protected void HandleDrag(UIPanGestureRecognizer recognizer)
{
PointF offset2 = recognizer.TranslationInView(currView);
// if it's just began, cache the location of the image


if (recognizer.State != (UIGestureRecognizerState.Cancelled | UIGestureRecognizerState.Failed
| UIGestureRecognizerState.Possible))
{
Console.WriteLine ("Here");
// NEED TO LOAD ANOTHER VIEW HERE
}

}
private void Initialize()
{
}
}

最佳答案

来自 Apple 文档:

平移手势是连续的。当允许的最小手指数 (minimumNumberOfTouches) 移动到足以被视为平移时,它开始 (UIGestureRecognizerStateBegan)。当至少按下最小数量的手指移动手指时,它会更改 (UIGestureRecognizerStateChanged)。当所有手指都抬起时结束(UIGestureRecognizerStateEnded)。

检查 UIGestureRecognizerState.Ended 状态,如果出现这种情况,您可以加载另一个 View 。根据您的描述,您似乎可以使用 UISwipeGestureRecognizer 而不是 UIPanGestureRecognizerUISwipeGestureRecognizer 是一个离散的手势,因此您不必检查状态,只需以这种方式实现它:

在您的代码中的某处实现处理程序:

private void HandleSwipeRight()
{
// load another view
Console.WriteLine("Swipe right.");
}

并在ViewDidLoad()中初始化手势识别器:

UISwipeGestureRecognizer swipeGestureRecognizerRight = new UISwipeGestureRecognizer(HandleSwipeRight);
swipeGestureRecognizerRight.Direction = UISwipeGestureRecognizerDirection.Right;
this.View.AddGestureRecognizer(swipeGestureRecognizerRight);

您可以为不同的方向添加另一个滑动手势识别器。

关于c# - Xamarin IOS 加载 View 表单手势事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765936/

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