我有一个很好用的签名控件,除非你的名字很长,否则它会开始显示空白。似乎与性能有关,但在模拟器和最新的 iPad 上都是一样的。我在下面附上了一个示例项目以及签名绘图代码。
任何帮助将不胜感激!
https://dl.dropboxusercontent.com/u/25670071/SignatureArchive.zip
using System;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;
namespace MyApp
{
public class SignatureViewV2 : UIView
{
public delegate void SignatureChanged();
public SignatureChanged OnSignatureChanged;
private bool _empty = true;
// clear the canvas
public void Clear ()
{
drawPath.Dispose ();
drawPath = new CGPath ();
fingerDraw = false;
SetNeedsDisplay ();
_empty = true;
}
public bool IsEmpty ()
{
return _empty;
}
public SignatureViewV2 (RectangleF frame) : base(frame)
{
this.drawPath = new CGPath ();
this.BackgroundColor = UIColor.White;
}
private PointF touchLocation;
private PointF prevTouchLocation;
private CGPath drawPath;
private bool fingerDraw;
public override void TouchesBegan (MonoTouch.Foundation.NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
UITouch touch = touches.AnyObject as UITouch;
this.fingerDraw = true;
this.touchLocation = touch.LocationInView (this);
this.prevTouchLocation = touch.PreviousLocationInView (this);
this.SetNeedsDisplay ();
}
public override void Draw (RectangleF rect)
{
base.Draw (rect);
if (this.fingerDraw) {
using (CGContext context = UIGraphics.GetCurrentContext()) {
context.SetStrokeColor (UIColor.FromRGB(63, 112, 185).CGColor);
context.SetLineWidth (2f);
context.SetLineJoin (CGLineJoin.Round);
context.SetLineCap (CGLineCap.Round);
this.drawPath.MoveToPoint (this.prevTouchLocation);
this.drawPath.AddLineToPoint (this.touchLocation);
context.AddPath (this.drawPath);
context.DrawPath (CGPathDrawingMode.Stroke);
}
if(OnSignatureChanged != null)
OnSignatureChanged();
_empty = false;
}
}
public override void TouchesMoved (MonoTouch.Foundation.NSSet touches, UIEvent evt)
{
base.TouchesMoved (touches, evt);
UITouch touch = touches.AnyObject as UITouch;
this.touchLocation = touch.LocationInView (this);
this.prevTouchLocation = touch.PreviousLocationInView (this);
this.SetNeedsDisplay ();
}
public UIImage GetDrawingImage ()
{
UIImage returnImg = null;
UIGraphics.BeginImageContext (this.Bounds.Size);
using (CGContext context = UIGraphics.GetCurrentContext()) {
context.SetStrokeColor (UIColor.FromRGB(63, 112, 185).CGColor);
context.SetLineWidth (5f);
context.SetLineJoin (CGLineJoin.Round);
context.SetLineCap (CGLineCap.Round);
context.AddPath (this.drawPath);
context.DrawPath (CGPathDrawingMode.Stroke);
returnImg = UIGraphics.GetImageFromCurrentImageContext ();
}
UIGraphics.EndImageContext ();
return returnImg;
}
}
}