gpt4 book ai didi

ios - 为什么在 Xamarin IOS customrenderer 中没有准确检测到长按开始/结束?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:01 24 4
gpt4 key购买 nike

我有一个 Xamarin Forms 项目,我想在其中有一个按钮,必须按 才能触发事件。除了长按事件,我还需要按下按钮的确切时间,因为那时我想在 UI 上启动一些动画。

我制作了一个自定义渲染器来解决它,在 Android 上它可以工作,但在 IOS 上我有问题:有时它可以工作,有时不能。

在 IOS 上我是这样做的:我使用 TouchDown 事件来获取触摸的确切时间,TouchUpInside 结束,UILongPressGestureRecognizer 长按。有时它就像一个魅力,但有时只有 UILongPressGestureRecognizer 的事件被触发而没有触发 TouchDown

这是我的代码:

[assembly: ExportRenderer(typeof(LongPressButton), typeof(LongPressButtonRenderer))]

namespace AnyProj.iOS
{
public class LongPressButtonRenderer : ButtonRenderer
{
private LongPressButton _longPressButton;

private UILongPressGestureRecognizer _longPressGestureRecognizer;

public LongPressButtonRenderer()
{

}

protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);

_longPressGestureRecognizer = new UILongPressGestureRecognizer(() => HandleLongPress());

if(e.NewElement == null)
{
if (_longPressGestureRecognizer != null)
{
this.RemoveGestureRecognizer(_longPressGestureRecognizer);
}
}

if(e.OldElement == null)
{
_longPressButton = (LongPressButton)e.NewElement;
Control.TouchDown += (sender, ee) => {
Debug.WriteLine("Sometimes triggered, sometimes not.");
};
Control.TouchUpInside += (sender, ee) => {
Debug.WriteLine("Sometimes triggered, sometimes not.");
};
_longPressGestureRecognizer.MinimumPressDuration = 2.0;
this.AddGestureRecognizer(_longPressGestureRecognizer);
}
}

private void HandleLongPress()
{
try
{
if (_longPressGestureRecognizer.State == UIGestureRecognizerState.Began)
Debug.WriteLine("Sometimes triggered, sometimes not.");
}
catch(Exception exc)
{
Debug.Assert(false,exc.Message);
}
}
}
}

你能帮我改进一下吗?非常感谢!

更新:它发生在真实设备 (iPhone SE) 上,在模拟器上它总是像一个魅力!

更新 2:我在 Swift 中做了一个小测试项目来实现这个功能,效果很好,所以没有像通过 Xamarin 表单那样出现类似的错误。

class ViewController: UIViewController {

@IBOutlet weak var longPressButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()

longPressButton.addTarget(self, action:#selector(touchDown), for: .touchDown)
longPressButton.addTarget(self, action:#selector(touchUp), for: .touchUpInside)

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
longGesture.minimumPressDuration = 2.0
longPressButton.addGestureRecognizer(longGesture)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@objc func touchUp(){
print("Touch up")
}

@objc func touchDown(){
print("Touch down")
}

@objc func longTap(_ sender: UIGestureRecognizer){
print("Long tap")
if sender.state == .ended {
print("UIGestureRecognizerStateEnded")
}
else if sender.state == .began {
print("UIGestureRecognizerStateBegan.")
}
}

最佳答案

我已经在我的项目中测试了以下代码,它运行良好。在 iPhone 5c 物理设备上测试。请注意,如果长按开始,将不会调用 HandleTouchUpInside;相反,您可以从 UILongPressGestureRecognizerUIGestureRecognizerState.Ended 状态确定结束。

protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);

if (e.OldElement != null)
{
RemovePressEvents();
}

if (e.NewElement != null)
{
AddPressEvents();
}
}

private void AddPressEvents()
{
_longPressGestureRecognizer = new UILongPressGestureRecognizer(HandleLongPress);
Control.TouchDown += HandleTouchDown;
Control.TouchUpInside += HandleTouchUpInside;
AddGestureRecognizer(_longPressGestureRecognizer);
}

private void RemovePressEvents()
{
Control.TouchDown -= HandleTouchDown;
Control.TouchUpInside -= HandleTouchUpInside;
RemoveGestureRecognizer(_longPressGestureRecognizer);
}

private void HandleTouchDown(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("TOUCH DOWN");
}

private void HandleTouchUpInside(object sender, EventArgs e)
{
// Note: will not be called if the Long Press begins. Instead,
// You can determine the end of the press from when HandleLongPress
// is called and UILongPressGestureRecognizer.State =
// UIGestureRecognizerState.Ended
System.Diagnostics.Debug.WriteLine("TOUCH UP INSIDE");
}

private void HandleLongPress(UILongPressGestureRecognizer o)
{
switch (o.State)
{
case UIGestureRecognizerState.Began:
System.Diagnostics.Debug.WriteLine("LONG PRESS BEGAN");
break;

case UIGestureRecognizerState.Ended:
System.Diagnostics.Debug.WriteLine("LONG PRESS ENDED");
break;
}
}

关于ios - 为什么在 Xamarin IOS customrenderer 中没有准确检测到长按开始/结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48446834/

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