-1

I want to know how to detect tap for my UIScrollView as circular shape instead of rect? can you please show me how with obj c??

i can detect tap like this:

 if (CGRectContainsPoint(scrollView.frame, tappedPoint)) {


                           NSLog(@"hello");


            }

but this treat it like rect shape and i want to detect the tap as circular.

  • 1
    Possible duplicate of [how to detect touches inside a circular view](http://stackoverflow.com/questions/14078392/how-to-detect-touches-inside-a-circular-view) – Shamas S Dec 05 '15 at 13:11

1 Answers1

1

From this answer :

If you have a circle with center (center_x, center_y) and radius radius, how do you test if a given point with coordinates (x, y) is inside the circle?

In general, x and y must satisfy (x - center_x)^2 + (y - center_y)^2 < radius^2.

Please note that points that satisfy the above equation with < replaced by == are considered the points on the circle, and the points that satisfy the above equation with < replaced by > are considered the outside the circle.

Now, its pretty simple to get the center and radius from a frame:

CGPoint center = frame.center;
CGFloat radius = frame.size.width/2;

Update : So ur code would be -

- (BOOL) isPoint:(CGPoint)point insideCircleFromRect:(CGRect)frame
{
  CGPoint center = frame.center;
  CGFloat radius = frame.size.width/2;

  //(x - center_x)^2 + (y - center_y)^2 < radius^2
  CGFloat lhs = pow((point.x-center.x),2) + pow((point.y-center.y),2);
  CGFloat rhs = pow(radius,2);

  if(lhs<rhs)
    return YES;
  else
    return NO;
}
Community
  • 1
  • 1
ShahiM
  • 3,179
  • 1
  • 33
  • 58