2013-09-14 6 views
0

Я использую UIPanGestureRecogniser по адресу UIButton s. Я хочу, чтобы обнаружить, когда два из них пересекаются следующим образом:Обнаружение перекрытия с UIPanGestureRecognizer

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded || 
    [(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateCancelled || 
    [(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateFailed) { 

    CGPoint fingerPoint; 

    for(id key in BluetoothDeviceDictionary) { 
     UIButton* btn = [BluetoothDeviceDictionary objectForKey:key]; 
     fingerPoint = [(UIPanGestureRecognizer*)sender locationInView:btn.superview]; 
    } 

    //BLUETOOTH SHARING 

    if (CGRectContainsPoint(All buttons from dictionary.frame, fingerPoint)) { 

     for the UIButton that has been overlapped do... 

Что в основном происходит, что пользователь перетаскивает UIButton на любой другой UIButton в серии UIButton с на экране, которые являются частью dictionary , Когда пользователь выпускает какой-либо из них, программа должна распознавать, какая из них перекрывается, и относительная key от dictionary.

Я могу указать только одну кнопку для CGRectContainsPoint, и я также не знаю, как понять, какая из кнопок была, и получить key от dictionary.

ответ

1

Try что-то вроде:

UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender; 

if(gesture.state == UIGestureRecognizerStateEnded || 
    gesture.state == UIGestureRecognizerStateCancelled || 
    gesture.state == UIGestureRecognizerStateFailed) { 

CGPoint dropPoint = [gesture locationInView:gesture.view.superview]; 

for(id key in BluetoothDeviceDictionary) { 
    UIButton* btn = [BluetoothDeviceDictionary objectForKey:key]; 

    if (CGRectContainsPoint(btw.frame, dropPoint)) { 
     // overlap - do something... 

     // maybe continue or return (if the loop shouldn't continue to test the other buttons 
    } 
} 
Смежные вопросы