我有一个基于this article的自由绘画课程。
public partial class HandDrawingView : ReactiveView, IViewFor<ColorPickerViewModel>
{
UIBezierPath path;
UIColor strokeColor = UIColor.Black;
UIImage incrementalImage;
CGPoint[] points = new CGPoint[5]; // we now need to keep track of the four points of a Bezier segment and the first control point of the next segment
int counter; // a counter variable to keep track of the point index
nfloat strokeWidth = 4;
public HandDrawingView(IntPtr handle) : base(handle)
{
}
public override void AwakeFromNib()
{
base.AwakeFromNib();
this.MultipleTouchEnabled = false;
this.BackgroundColor = UIColor.White;
path = new UIBezierPath();
}
public override void Draw(CGRect rect)
{
base.Draw(rect);
if (incrementalImage != null)
{
incrementalImage.DrawAsPatternInRect(rect);
}
if (this.strokeColor != null)
{
this.strokeColor.SetStroke();
path.Stroke();
}
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
Debug.WriteLine("began");
counter = 0;
path.LineWidth = strokeWidth;
path.LineCapStyle = CGLineCap.Round;
var touch = touches.AnyObject as UITouch;
points[0] = touch.LocationInView(this);
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
Debug.WriteLine("move");
var touch = touches.AnyObject as UITouch;
var p = touch.LocationInView(this);
counter++;
points[counter] = p;
if (counter == 4)
{
// move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
points[3] = new CGPoint((points[2].X + points[4].X) / 2, (points[2].Y + points[4].Y) / 2);
path.MoveTo(points[0]);
path.AddCurveToPoint(points[3], points[1], points[2]);
this.SetNeedsDisplay();
points[0] = points[3];
points[1] = points[4];
counter = 1;
}
}
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
Debug.WriteLine("end");
this.DrawBitmap();
this.SetNeedsDisplay();
path.RemoveAllPoints();
counter = 0;
}
public override void TouchesCancelled(NSSet touches, UIEvent evt)
{
base.TouchesCancelled(touches, evt);
Debug.WriteLine("cancel");
this.TouchesEnded(touches, evt);
}
void DrawBitmap()
{
UIGraphics.BeginImageContextWithOptions(this.Bounds.Size, false, 0f);
if (incrementalImage != null)
{
incrementalImage.Draw(CGPoint.Empty);
}
this.strokeColor.SetStroke();
path.Stroke();
incrementalImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
}
public void ClearAll()
{
counter = 0;
points = new CGPoint[5];
path.RemoveAllPoints();
SetNeedsDisplay();
}
public ColorPickerViewModel _viewModel;
public ColorPickerViewModel ViewModel
{
get { return _viewModel; }
set { this.RaiseAndSetIfChanged(ref _viewModel, value); }
}
object IViewFor.ViewModel
{
get { return ((IViewFor<ColorPickerViewModel>)this).ViewModel; }
set { ((IViewFor<ColorPickerViewModel>)this).ViewModel = (ColorPickerViewModel)value; }
}
}
此课程以前在iOS 10中有效。自从我更新到iOS 11以来,它已不再适用了。如果我只使用一根手指,则会按以下顺序调用触摸事件:开始 - 移动 - 取消 - 结束,即使我的手指仍在移动且视图中没有绘制任何内容。
但如果我用两根手指(或更多),那么我可以用第一根手指触摸视图来绘制视图。触摸序列将变为这样:开始 - 移动 - 移动 - 移动 - {很多移动} - 移动 - 取消 - 结束
更新
我尝试将segue从Show(push)更改为以模态方式呈现,现在它可以正常工作。
为什么不使用segue Show(推送)?