Determine when a UIView is being dragged over a UICollectionViewCell

I have a drag-and-drop view (UIImageView) that I control its positioning and drag using UIPanGestureRecognizer and this method:

- (void)imageDragged:(UIPanGestureRecognizer *)gesture
{
    UIImageView *img = (UIImageView *)gesture.view;
    CGPoint translation = [gesture translationInView:img];
    img.center = CGPointMake(img.center.x + translation.x,
                           img.center.y + translation.y);
    // reset translation
    [gesture setTranslation:CGPointZero inView:img];
}

Besides the drag-and-drop view, I also have a UICollectionView built, of course, by UICollectionViewCells. What I'm trying to do is determine when my draggable view is dragged over one of the collection view cells.

I was thinking about adding the imageDragged boolean method to find out when a view is being dragged, but I could not figure out how to know when my drag is on top of a collection view cell. UICollectionView has didSelect delegation method, but it really doesn’t help me if my crane didn’t appear in cells, but on my draggable view.

Any help would be greatly appreciated!

+4
source share
2 answers

, , , , / UIView, CGRectContainsPoint, , .

+1

imageDropped , ( ):

CGPoint tapCoordinates = [gesture locationInView:self.view];
NSArray * visibleCells = [self.collectionView visibleCells];
for(UICollectionViewCell *cell in visibleCells) {
    CGRect frame = [self.collectionView convertRect:cell.frame toView:self.view];
    if(CGRectContainsPoint(frame, tapCoordinates)) {
        NSLOG(@"Success");
        break;
    }
}
+2

Source: https://habr.com/ru/post/1539638/


All Articles