Ríomhaire
Ríomhaire

Reputation: 3114

Drawing Circle at CGPoint touchPoint

I am trying to draw a circle (small like a dot) at the exact point of a UITouch.

The problem is, when I get the touchPoint from the user touch, I have to then draw the Rect in which to place the circle.

To work out an origin for the Rect, to place it such that, a circle drawn within it will have a centre point at the original touchpoint, I have used Pythagorus' theorem.

However, this approach is not elegant and is not exact.

Is there a better way to do this?

UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.view];

CGContextAddEllipseInRect(context,(CGRectMake ((touchPoint.x - 5.7), (touchPoint.y - 5.7)
                                                   , 9.0, 9.0)));

Upvotes: 1

Views: 2252

Answers (2)

vignesh kumar
vignesh kumar

Reputation: 2330

you can do like below

  CFFloat radius=10;
  UITouch *touch = obj;
  CGPoint touchPoint = [touch locationInView:self.view];

  CGContextAddEllipseInRect(context,(CGRectMake ((touchPoint.x - radius/2), (touchPoint.y 
                                              - radius/2)
                                               , radius, radius)));

Upvotes: 3

danh
danh

Reputation: 62686

I think your approach is fine. What could be more elegant than Pythagorus? As for accuracy, add a few more digits to the constants and you can be more accurate than both the user's sense of his own finger position and the system calculations giving you the centroid of the touch.

What's more, I can't think of a good alternative. (Maybe one: do this with an image view, where the image is a circle, and set the view's anchor point so that it positions in the middle of the touch).

Upvotes: 0

Related Questions