Question No. 1: The user must be able to drag another view over it, and when you delete this object, it will snap to the squares of the grid.
Suppose you drag a UIView. On touches made in UIView, I would use the center property, which contains the values ββof the xIV y coordinate of the UIView and passes this to a function that checks which square of the grid is inside.
It will look like this (suppose UIView is the name of a draggingView):
for (CGRect gridSquare in gridArray) { if (CGRectContainsPoint(gridRect, draggingView.center)) {
Now, if you're curious about what gridArray is, it's an array of all the grid squares that make up your game panel. If you need help creating this, let me know.
Question No. 2: The user should be able to iterate over each square in the grid and determine whether the object is inside this square of the grid.
If you could follow the above, then it is quite simple. Iterating over the squares of the grid, you can use the gridSquare start values ββto see if any of the draggingView subzones have the same origin. Using this, you can return a UIView that is inside a specific square. See below:
- (UIView *)findViewInSquare { for (CGRect rect in gridArray) { for (UIView *view in [self.view subViews]) { if (CGPointEqualToPoint(view.frame.origin, rect.origin)) { return view;
Hope this all makes sense, and let me know if you need any clarification.
source share