Draw Target Image with Cocoa Touch

With Cocoa Touch in Objective-C, I'm looking for the optimal way to draw a target image, (i.e. circles within circles, simple enough), and then have the user's touch recorded on the image, (potentially as an x or + mark), and more importantly, in memory for later re-drawing.

I'll be using a loupe for when the user holds their finger down for prolonged periods to enable more precise positioning, which I've learnt by reading and experimenting with the CoffeeShopped article and source.

Answers


Create a subclass of uiview and implement drawRect

- (void)drawRect:(CGRect)rect {
    CGRect circleRect = self.bounds;

    CGFloat circleWidth = circleRect.size.width / 5.0;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillEllipseInRect(context, circleRect);

    circleRect = CGRectInset(circleRect, circleWidth, circleWidth);
    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextFillEllipseInRect(context, circleRect);        

    circleRect = CGRectInset(circleRect, circleWidth, circleWidth);
    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillEllipseInRect(context, circleRect);
}

Will draw

You should set you're views backgoundColor to [UIColor clearColor] to make it not black around the edges.

You could also tweak this into a loop but that is the simplest example code i can show.

Note: I've not reused the colors, for simplicity of arc/nonarc code


Need Your Help

Rotate imageView as per touch

ios cgaffinetransform touchmove

I am rotating imageView which is minuit hand of a clock, with

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.