使用UIPanGestureRecognizer检测重叠

时间:2013-09-14 17:45:40

标签: ios

我在UIPanGestureRecogniser上使用UIButton。我想通过以下方式检测其中两个重叠:

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中的任何其他dictionarykey。当用户发布其中任何一个时,程序必须识别其中哪一个已重叠,并且dictionary中的相对CGRectContainsPoint

我只能为key指定一个按钮,我也不知道如何理解它是哪一个按钮,并从dictionary获取{{1}}。

1 个答案:

答案 0 :(得分:1)

尝试类似:

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
    }
}