Fire UIPanGestureRecognizer with external starting point

I have a layout as shown below:

question image

The black area is the camera, the red area is only a UIView with a UIPanGestureRecognizer, and the green area is a UICollectionView with images.

Right now, if the user taps the red area and starts dragging the entire layout, it will move to the top and the user can see more items in the collection view at the same time when the camera fills the device screen. All logic has already been executed.

My problem is that I want the same functionality even if the user starts dragging outside the red area, that is, the user starts dragging the collection view from the bottom of the device so that more items are shown, but if the user reaches (crosses, moves) the red area, then the PanGestureRecognizer should be fired. Instagram has this feature when selecting an image from disk. Is there a way to achieve this that I can't see? I tried to override the red view with pointInside, touchsBegan and touchsMoved, but either of them gets called if the drag is from the collection view. SwipeGesture doesn't work either. Thank!

+3


source to share


1 answer


You can try this:

1) Add the PanGesture (red) you want to activate as a collection:

[self.collectionView addGestureRecognizer:redPanGesture];

      

2) In the method that is called when PanGesture is enabled, filter it so that the logic only runs when the finger is over the red area:

CGPoint location = [recognizer locationInView:self.redView];

if(location.y > self.redView.frame.size.height)
{
    [(UIPanGestureRecognizer *)recognizer setTranslation:CGPointZero inView:self.view];
    return;
}

      



3) Set your class as PanGesture delegate:

redPanGesture.delegate = self;

      

4) Implement the method below, otherwise the collection view will not scroll:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

      

+3


source







All Articles