MonoTouch中非常简单的测试视图使用Core Graphics绘制一条线,但未显示视图内容

时间:2011-01-13 21:40:03

标签: cocoa-touch xamarin.ios

我现在放弃了这个非常简单的测试,我一直试图运行。我想在我的窗口添加一个子视图,除了从iPhone的屏幕的一个角落到另一个角落之外什么都没做,然后使用touchesMoved()它应该从最后一个点到当前点画一条线。 问题: 1.初始线已经不可见了。 2.使用Interface Builder时,初始行是可见的,但是即使我调用SetNeedsDisplay(),也不会调用drawRect()。

它可能不是那么难......有人可以修改下面的代码使它工作吗?

在FinishedLaunching()中的main.cs中:

oView = new TestView();
oView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
oView.Frame = new System.Drawing.RectangleF(0, 0, 320, 480);
window.AddSubview(oView);
window.MakeKeyAndVisible ();

TestView.cs:

using System;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;
using MonoTouch.CoreAnimation;
using MonoTouch.Foundation;
namespace Test
{
    public class TestView : UIView
    {
        public TestView () : base()
        {
        }

        public override void DrawRect (RectangleF area, UIViewPrintFormatter formatter)
        {
            CGContext oContext = UIGraphics.GetCurrentContext();
            oContext.SetStrokeColor(UIColor.Red.CGColor.Components);
            oContext.SetLineWidth(3.0f);
            this.oLastPoint.Y = UIScreen.MainScreen.ApplicationFrame.Size.Height - this.oLastPoint.Y;
            this.oCurrentPoint.Y = UIScreen.MainScreen.ApplicationFrame.Size.Height - this.oCurrentPoint.Y;
            oContext.StrokeLineSegments(new PointF[] {this.oLastPoint, this.oCurrentPoint });
            oContext.Flush();
            oContext.RestoreState();
            Console.Out.WriteLine("Current X: {0}, Y: {1}", oCurrentPoint.X.ToString(), oCurrentPoint.Y.ToString());
            Console.Out.WriteLine("Last X: {0}, Y: {1}", oLastPoint.X.ToString(), oLastPoint.Y.ToString());

        }

        private PointF oCurrentPoint = new PointF(0, 0);
        private PointF oLastPoint = new PointF(320, 480);

        public override void TouchesMoved (MonoTouch.Foundation.NSSet touches, UIEvent evt)
        {
            base.TouchesMoved (touches, evt);
            UITouch oTouch = (UITouch)touches.AnyObject;
            this.oCurrentPoint = oTouch.LocationInView(this);
            this.oLastPoint = oTouch.PreviousLocationInView(this);
            this.SetNeedsDisplay();
        }
    }
}

1 个答案:

答案 0 :(得分:6)

你覆盖了错误的Draw方法。

您覆盖了用于打印的DrawRect (RectangleF, UIViewPrintFormatter)

您想要覆盖Draw (RectangleF)