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];
...
}