0

In my application I have many UIButtons dynamically added to the View and I use below method to Drag them around the View.

//forDragAction
 [btnTarget addTarget:self action:@selector(wasDragged:withEvent:)
                forControlEvents:UIControlEventTouchDragInside];

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
    // get the touch
    UITouch *touch = [[event touchesForView:button] anyObject];

    // get delta
    CGPoint previousLocation = [touch previousLocationInView:button];
       // frameof buttonChanged here
}

I want to stop dragging action if the dragged one intersect with anyother, I know that I can use for loop like below to check if any UIButton is Interacting

for(UIButton *btn in [[button superview] subViews])
{
    //check if the btn frame interact with any others if so comeout of loop
}

I want to know if there is someother way, As mentioned way will get slower if the subViews count increse to such great amount

Edit:- the UIButtons are dynamically added to the UIView (But total amount of subViews won't exceed 120)

vignesh kumar
  • 2,310
  • 2
  • 25
  • 39

2 Answers2

1

Try brute force first. You might be surprised at how well it does. (But remember that a loop through [[button superview] subviews] will contain the button itself, so it will always stop because the button intersects itself. Be sure to exclude the button).

Optimize after you have something working that is demonstrably slow with real data.

If that's really the case, there's a whole lot of algorithmic work done on this problem, which can be summarized as preprocessing the data into structures that allow cheaper initial tests to reject distant objects. This is a good SO answer on the topic, referring to this article.

Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136
0

I don't think that count of subviews, that may fill the screen (without intersections), is too large. So use function:

bool CGRectIntersectsRect (
   CGRect rect1,
   CGRect rect2
);

for detect whether frame of dragged button intersects with another subview.

Mikhail
  • 4,271
  • 3
  • 27
  • 39