Question

So I'm trying to implement drag and drop with image thumbnails acquired using the ALAssets Framework.

I'm able to get the long press gesture to work properly (its definitely moving something around based on some NSLog testing) but can't for the life of me figure out how to actually display the thumbnail image and drag it around in the main view. Note that _dNewImageView was a IBOutlet added as a property to the view controller. I'm trying to bring the view to front with the bringSubviewToFront: however its not working as expected. Any help would be appreciated!

Here is the function for handling the long press gesture:

-(void)longGestureRecognized:(UILongPressGestureRecognizer *)gestureRecognizer{
    gestureRecognizer.delaysTouchesBegan = YES;
    CGPoint newPoint = [gestureRecognizer locationInView:self.collectionView];

    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
            dIndexPath = [self.collectionView indexPathForItemAtPoint:newPoint];
            if (dIndexPath == nil){
                NSLog(@"Couldn't find index path");
            }
            dCell = (SKPhotoCell *)[self.collectionView cellForItemAtIndexPath:dIndexPath];
            dImage = [UIImage imageWithCGImage:[dCell.asset thumbnail]];
            _dNewImageView = [[UIImageView alloc] initWithImage:dImage];
            [_dNewImageView setUserInteractionEnabled:YES];
            [[self view] bringSubviewToFront:_dNewImageView];
            break;

        case UIGestureRecognizerStateChanged:
            [_dNewImageView setCenter:newPoint];
            break;

        case UIGestureRecognizerStateEnded:
            break;
    }
}
Was it helpful?

Solution

Two things you need to add:

[self.view insertSubview:_dNewImageView aboveSubview:self.collectionView]; wherever you allocate and initialize _dNewImageView.

and

[gestureRecognizer setTranslation:CGPointZero inView:gestureRecognizer.view]; within your gesture recognition method.

If you are reusing _dNewImageView as your code suggests, you shouldn't allocate and initialize it each time the method is called. Use [_dNewImageView setImage:dImage];

Additionally, when you are checking if the indexPath exists, be sure to implement some way for the method to return or bypass the image stuff.

For example:

- (void) someOtherMethod {
    _dNewImageView = [[UIImageView alloc] init];
    _dNewImageView.frame = CGRectSomething;
    _dNewImageView.userInteractionEnabled = YES;
    [self.view insertSubview:_dNewImageView aboveSubview:self.collectionView];
}

- (void) longGestureRecognized:(UILongPressGestureRecognizer *)gestureRecognizer {
    ...
        case UIGestureRecognizerStateBegan:
            ...
            if (dIndexPath == nil) {
                NSLog(...);
                return; /*or*/ goto noIndexPath;
            }
            ...
        break;

    //if used 'goto'
    noIndexPath:
        [gestureRecognizer setTranslation:CGPointZero inView:gestureRecognizer.view];

    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top