iOS - Drag and drop collision detection How to detect when your selected item drags over another subview?

前端 未结 2 1998
遥遥无期
遥遥无期 2021-02-03 12:49

We are adding drag and drop functionality to what is to become a sports field with positions for players.

The positions are mapped out using Interface Builder with each

相关标签:
2条回答
  • 2021-02-03 13:31

    check if the frame of the item you are moving intersects with the frame from on of your subviews

    for (UIView *anotherView in self.subviews) {
        if (movingView == anotherView)
            continue;
        if (CGRectIntersectsRect(movingView.frame, anotherView.frame)) {
            // Do something
        }
    }
    

    If I were you, I would add all game relevant items to an NSArray and for-loop through this. So you don't detect collisions with subviews that have nothing to do with your game like labels and so on.

    0 讨论(0)
  • 2021-02-03 13:34

    Also might want to consider view.center with CGRectContainsPoint()

    0 讨论(0)
提交回复
热议问题