gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:54:44 24 4
gpt4 key购买 nike

我创建了一个如下所示的 Canvas View 。当我将它应用到新项目时,它可以正常工作,但是当我将它放入我的真实项目时,我无法绘制实线。它总是被虚线(如下图所示)。第一次,我认为这是一个问题,因为我推了太多屏幕,直到出现这个可绘制对象。但每次我插入新 View 时,我都尝试禁用前一个 View 的 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);
}
}
}
}

the screen

最佳答案

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

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

关于c# - TouchMove 事件在 xamarin ios 中结束得如此之快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33781504/

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