2

I have a grid of small (30X30 size) UIViews & I am drawing a line over them by tapping two points on the screen as startpoint & endpoint by using the code below:

CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {244.0f/255.0f, 226.0f/255.0f, 119.0f/255.0f, 0.8};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineWidth(context, 20.0);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);

Tapping two points on screen & drawing a line is working fine but how do I get all the views intersected by the line? I want to get these views in the touches ended method.

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  CGPoint pt = [[touches anyObject] locationInView:alphabetView];
  UIView *touched = [alphabetView hitTest:pt withEvent:event];
  CGPoint p = touched.center;
  // code here to get view list.
}

Any help would be appreciated.

ganzogo
  • 2,516
  • 24
  • 36
stackNeverFlow
  • 203
  • 4
  • 16
  • try CGRectIntersectRect function & passed your line frame & view frame to it. – Girish Apr 17 '13 at 06:09
  • ok but how can i get the line frame ? And i have about 80 views on the screen & should i check all of them one by one? Could you please provide an example ? – stackNeverFlow Apr 17 '13 at 06:19
  • do you have list of views in array, which you have created? – Dipen Panchasara Apr 17 '13 at 09:47
  • No i dont have the list of view in array, but all these views are added in a bigger view & marked with tag. i can find all the views by using : for (UIView *view in alphabetView.subviews) { // code to get tag here } – stackNeverFlow Apr 18 '13 at 03:25
  • @x-men: It would be nice to know if my answer was of any use :-) – Martin R Apr 23 '13 at 08:17
  • yes @Martin your answer was knowledgeful, thanx but i was looking for the code. I got busy with my project so i am replying late. I solved my problem another way by finding the center points of all the views by which the line is passing. Actually i already have the views of definite size so it was easy for me to find the views which came across the line . – stackNeverFlow May 01 '13 at 09:44

2 Answers2

1

A line segment (from point A to point B) intersects a rectangle (the view frame) if:

  1. the line segment intersects any of the the 4 rectangle sides, or
  2. both points A and B are inside the rectangle.

If the answer to both (1) and (2) is NO, then the line segment does not intersect the rectangle.

The function checkLineIntersection from this answer might be useful to check for condition (1).

CGRectContainsPoint() can be used to check for condition (2).

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

You can use CGContextPathContainsPoint() to check whether your CGPoint is in the path of your line, it returns a boolean value. its reference can be found here

BhushanVU
  • 3,455
  • 1
  • 24
  • 33
  • ok , but how do i check that which CGPoint to check? i dont have points list. As i am thinking , I need to get all the points in the line itself & then check if these points contained in my view or not. But i m not sure how do i get all the points in the line . But this would be a lengthy & slow process . There should be a faster way. – stackNeverFlow Apr 17 '13 at 06:39
  • as you said you want these views in touches ended and you have CGPoint p. – BhushanVU Apr 17 '13 at 07:03
  • i have CGPoint p for the start point And endpoint only.i don't have all the points between the start & the end points – stackNeverFlow Apr 17 '13 at 07:18