Domanda

I have a draggable view (UIImageView) which I control its positioning and dragging using the 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];
}

Other than the draggable view, I also have a UICollectionView, built of course by UICollectionViewCells. What Im trying to do, is to identify when my draggable view is dragged on top of one of the collection view cells.

I thought of adding a boolean to the imageDragged method, to know when the view is currently being dragged, but I wasn't able to figure out how to know when my dragged touch is on top of a collection view cell. The UICollectionView has the didSelect delegate method, but that doesn't really help me if my tap didn't occur on the cells, but on my draggable view.

Any help would be much appreciated!

È stato utile?

Soluzione 2

I don't know if there's a tidier way, but perhaps you can get the point of the tap and/or some points on the corners of the dragged UIView and then use CGRectContainsPoint to see if those points are within the rect of the collection view cell.

Altri suggerimenti

I put this snippet inside the imageDropped method to achieve what I wanted (thanks to Martin's help):

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;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top