我创建了如下的画布视图。当我将其应用于新项目时,它可以正常工作,但是当我将其放入真实项目中时,我无法画出一条实线。它总是虚线(如下面的图片)。第一次,我认为这是一个问题,因为我在此可绘制对象之前推了太多的屏幕。但是,每当我向其中推入新视图时,都试图禁用上一个视图的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/