0

视图控制器的类:

namespace DrawOverride
    {
        public partial class DrawOverrideViewController : UIViewController
        {
            float W, H;
            CGPoint initialPoint;
            Drawer dr;

            public DrawOverrideViewController (IntPtr handle) : base (handle)
            {
            }

            public DrawOverrideViewController ()
            {

            }

            public override void DidReceiveMemoryWarning ()
            {
                // Releases the view if it doesn't have a superview.
                base.DidReceiveMemoryWarning ();

                // Release any cached data, images, etc that aren't in use.
            }

            #region View lifecycle

            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
                initialPoint = new CGPoint(5, 5);
                dr = new Drawer (initialPoint);
                **Console.WriteLine("1 - "+initialPoint);**
                // Perform any additional setup after loading the view, typically from a nib.
            }

            public override void ViewWillAppear (bool animated)
            {
                base.ViewWillAppear (animated);
            }

            public override void ViewDidAppear (bool animated)
            {
                base.ViewDidAppear (animated);
            }

            public override void ViewWillDisappear (bool animated)
            {
                base.ViewWillDisappear (animated);
            }

            public override void ViewDidDisappear (bool animated)
            {
                base.ViewDidDisappear (animated);
            }

            #endregion

            public override void TouchesBegan (NSSet touches, UIEvent evt)
            {
                base.TouchesBegan (touches, evt);
                UITouch touch = touches.AnyObject as UITouch;
        }
    }

第二类:

namespace DrawOverride
{
    partial class Drawer : UIView
    {
        CGPath pathed;

        CGPoint initialPoint;
        CGPoint latestPoint;

        public Drawer (IntPtr handle) : base (handle)
        {
        }

        public Drawer (CGPoint a)
        {
            initialPoint = a;
            **Console.WriteLine ("2 - "+a);**
        }

        public override void TouchesBegan (NSSet touches, UIEvent evt)
        {
            base.TouchesBegan (touches, evt);
        }

        public override void TouchesMoved (NSSet touches, UIEvent evt)
        {
            base.TouchesMoved (touches, evt);
            UITouch touch = touches.AnyObject as UITouch;
            latestPoint = touch.LocationInView (this);
            SetNeedsDisplay ();
        }

        public override void TouchesEnded (NSSet touches, UIEvent evt)
        {
            base.TouchesMoved (touches, evt);
        }

        public override void Draw (CGRect rect)
        {
            base.Draw (rect);
            pathed = new CGPath ();

            using (CGContext g = UIGraphics.GetCurrentContext ()) {
                g.SetLineWidth (8);
                UIColor.Blue.SetFill ();
                UIColor.White.SetStroke ();
                **Console.WriteLine("3 - "+initialPoint);**
                if (pathed.IsEmpty) {
                    pathed.AddLines (new CGPoint []{ initialPoint, latestPoint });
                } else {
                    pathed.AddLineToPoint (latestPoint);
                }

                //add geometry to graphics context and draw it
                g.AddPath (pathed);
                g.DrawPath (CGPathDrawingMode.FillStroke);
            }
        }
    }
}

在“Console.WriteLine”1 和 2 处,变量仍然有一个值,(5, 5),就像我做的那样。在 3,在被覆盖的 draw 方法中,值的打印是 {0,0}。我也尝试过使用:

抽屉博士 = 新抽屉();dr.initialPoint = 初始点;

但它也不起作用..

4

0 回答 0