TouchMove事件在xamarin ios中结束得如此之快

本文关键字:结束 如此之 ios 事件 xamarin TouchMove | 更新日期: 2023-09-27 18:22:20

我创建了一个画布视图,如下所示。当我把它应用到一个新项目中时,它是正确的,但当我把它们应用到我的真实项目中时时,我无法画出一条实线。它总是被打破(如下图所示)。第一次,我认为这是一个问题,因为我推了太多的屏幕,直到这个绘图。但每次我推送一个新视图时,我都试图禁用上一个视图的UserInteraction,但它不起作用。请帮忙!

public class CanvasView : UIView
{
    public CanvasView (CGRect frame) : base(frame)
    {
        this.drawPath = new CGPath();
        BackgroundColor = UIColor.White;
    }

    private CGPoint touchLocation;
    private CGPoint previousTouchLocation;
    private CGPath drawPath;
    private bool fingerDraw;
    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();
        Layer.BorderWidth = 0.5f;
        Layer.BorderColor = UIColor.FromRGB (146, 146, 146).CGColor;
    }
    public override void TouchesBegan (NSSet touches, UIEvent evt)
    {
        base.TouchesBegan (touches, evt);
        UITouch touch = touches.AnyObject as UITouch;
        this.fingerDraw = true;
        this.touchLocation = touch.LocationInView(this);
        this.previousTouchLocation = touch.PreviousLocationInView(this);
        this.SetNeedsDisplay();
    }
    public override void TouchesMoved (NSSet touches, UIEvent evt)
    {
        base.TouchesMoved (touches, evt);
        UITouch touch = touches.AnyObject as UITouch;
        this.touchLocation = touch.LocationInView(this);
        this.previousTouchLocation = touch.PreviousLocationInView(this);
        this.SetNeedsDisplay();
    }
    public override void TouchesEnded (NSSet touches, UIEvent evt)
    {
        base.TouchesEnded (touches, evt);
        var alert = new UIAlertView () {
            Message = "Xui"
        };
        alert.AddButton("OK");
        alert.Show();
    }
    public override void Draw (CGRect rect)
    {
        base.Draw (rect);
        if (this.fingerDraw)
        {
            using (CGContext context = UIGraphics.GetCurrentContext())
            {
                context.SetStrokeColor(UIColor.Black.CGColor);
                context.SetLineWidth(1.5f);
                context.SetLineJoin(CGLineJoin.Round);
                context.SetLineCap(CGLineCap.Round);
                this.drawPath.MoveToPoint(this.previousTouchLocation);
                this.drawPath.AddLineToPoint(this.touchLocation);
                context.AddPath(this.drawPath);
                context.DrawPath(CGPathDrawingMode.Stroke);
            }
        }
    }
}

屏幕

TouchMove事件在xamarin ios中结束得如此之快

我认为问题不在于TouchesMoved,而在于您的绘图逻辑。当你调用SetNeedsDisplay()时,它的意思不是"现在绘制",而是"我有需要绘制的更改,所以亲爱的,尽可能地绘制我。"这意味着在处理绘图之前,会有更多的触摸。

你必须保持前一个点不变,直到你得到对Draw的调用,或者你必须将这些点放入缓冲区,并同时绘制它们。我会做第一个,因为它更简单。