Very simple test view in MonoTouch draws a line using Core Graphics but view content is not shown
Posted
by
Krumelur
on Stack Overflow
See other posts from Stack Overflow
or by Krumelur
Published on 2011-01-13T21:40:03Z
Indexed on
2011/01/13
21:53 UTC
Read the original article
Hit count: 211
cocoa-touch
|monotouch
Hi,
I give up now on this very simple test I've been trying to run. I want to add a subview to my window which does nothing but draw a line from one corner of the iPhone's screen to the other and then, using touchesMoved() it is supposed to draw a line from the last to the current point. The issues: 1. Already the initial line is not visible. 2. When using Interface Builder, the initial line is visible, but drawRect() is never called, even if I call SetNeedsDisplay().
It can't be that hard...can somebody fix the code below to make it work?
In main.cs in FinishedLaunching():
oView = new TestView();
oView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
oView.Frame = new System.Drawing.RectangleF(0, 0, 320, 480);
window.AddSubview(oView);
window.MakeKeyAndVisible ();
The 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();
}
}
}
© Stack Overflow or respective owner