0

I have a UIScrollView which is able to contains many view. To allow a good scrolling (without the content going outside the view while scrolling), on my Main.sotryboard , I've clicked on my UIScrollView and then in the attribute inspector I have allowed the Clip Subviews property:

The 3rd property is: Clip Subviews

My problem: all the views which are in my UIScrollViews are draggable (because they all have a UIPanGestureRecognizer. So, when I try to drag them OUTSIDE my UIScrollView, they just disappear. In fact they're just going behind every other view

To give you an exemple, I have others components which allow the drop of a view form the precedent UIScrollView. So when I begin the drag'n'drop from it, it disappear, and then reappear in the second component on which I have dropped the view.

What I have tried: I have a special UIPanGestureRecognizer for te drag'n'drop of a view coming from this UIScrollView. So, I actually have this (which, obviously, doesn't work, otherwise I would not be here):

//Here recognizer is the `UIPanGestureRecognizer`
//selectpostit is the name of the view I want to drag
if(recognizer.state == UIGestureRecognizerStateBegan){
    selectpostit.clipsToBounds = NO;
}

Any Ideas on how I can improve that? Thanks in advance.

Kokodelo
  • 416
  • 1
  • 5
  • 25
  • setting clipsToBounds to NO should be done for the parent view. in your case scroll view. – Surya Subenthiran Jun 03 '16 at 10:19
  • 1
    Instead of changing the `clipsToBounds` on the view you want to drag, change it on the `scrollView`. Another option which worked for me when dragging views is to create and control an image snapshot of the view, then hide the original view. When you are done with the dragging, reposition where necessary – Cjay Jun 03 '16 at 10:29
  • Oh that's looks really great! Thanks. But now, when I have a lot of view inside of my Scrollview, they appear while i'm dragging (I have done the reverse when the drag is finished so clipToBounds = yes if(recognizer.state == UIGestureRecognizerStateEnded). Do you have a solution for this? @Surya Subenthiran – Kokodelo Jun 03 '16 at 10:31
  • If I take a snapshot of my scroll view, should it resolve the problem i've mentioned below? @cjay – Kokodelo Jun 03 '16 at 10:32
  • A snapshot of the view you are dragging not the scroll. However, I think you can attack it both. Also you can try calling right the layout function (`setNeedsDisplay` etc...) to redraw the scroll view at the end of the gesture – Cjay Jun 03 '16 at 10:39

2 Answers2

1

You could try to reset scrollView.clipsToBounds to NO every time gesture starts, but that would lead to side effect when other content outside scroll view would become visible when dragging is in the progress.

I would recommend to take snapshot of the the draggable view when panning starts, place it on the scrollview's parent, and move it. Such approach should solve your problem.

Here is the code:

- (void)onPanGesture:(UIPanGestureRecognizer*)panRecognizer
{
    if(panRecognizer.state == UIGestureRecognizerStateBegan)
    {
        //when gesture recognizer starts, making snapshot of the draggableView and hiding it
        //will move shapshot that's placed on the parent of the scroll view
        //that helps to prevent cutting by the scroll view bounds
        self.draggableViewSnapshot = [self.draggableView snapshotViewAfterScreenUpdates: NO];
        self.draggableView.hidden = YES;
        [self.scrollView.superview addSubview: self.draggableViewSnapshot];
    }

    //your code that updates position of the draggable view

    //updating snapshot center, by converting coordinates from draggable view
    CGPoint snapshotCenter = [self.draggableView.superview convertPoint:self.draggableView.center toView: self.scrollView.superview];
    self.draggableViewSnapshot.center = snapshotCenter;

    if(panRecognizer.state == UIGestureRecognizerStateEnded ||
       panRecognizer.state == UIGestureRecognizerStateCancelled ||
       panRecognizer.state == UIGestureRecognizerStateFailed)
    {
        //when gesture is over, cleaning up the snapshot
        //and showing draggable view back
        [self.draggableViewSnapshot removeFromSuperview];
        self.draggableViewSnapshot = nil;
        self.draggableView.hidden = NO;
    }
}
1

I recommend you look at this article by ray wenderlich Moving Table View Cells with a Long Press Gesture

It explains how to create snapshots

Cjay
  • 1,093
  • 6
  • 11
  • Thanks a lot i'll have a look, but you should at this answer as a comment no? This isn't actually my initial problem. – Kokodelo Jun 03 '16 at 10:53