SagittariusA
SagittariusA

Reputation: 5427

How to disable scrolling while using UIPanGestureRecognizer on a UIView

In my app I have a draggable UIView contained in a UIScrollView so that I can zoom it. This moving UIView has a UIPanGestureRecognizer but if I zoom in first and then I try to move the UIView, that does not happen and instead the scrolling is performed as if the UIPanGestureRecognizer is not detected.

enter image description here

This is my associated method to the UIPanGestureRecognizer:

func handlePan(recognizer: UIPanGestureRecognizer) {
    if (self.panRec.state == UIGestureRecognizerState.Began) {
        self.scrollViewContainer.scrollEnabled = false
    }

    else if (self.panRec.state == UIGestureRecognizerState.Ended) {
        self.scrollViewContainer.scrollEnabled = true
    }

    let translation = recognizer.translationInView(self)
    if let view = recognizer.view {
        self.center = CGPoint(x:view.center.x + translation.x,
            y:view.center.y + translation.y)
    }
    recognizer.setTranslation(CGPointZero, inView: self)
    self.updatePinsPosition()

}

As you can see I try to disable scrolling when the the user taps in the draggable UIView and reactivate it at the end but it doesn't work. Where am I making a mistake? What am I missing?

UPDATE So explain myself better, I have a UIScrollView which contains a UIView and inside this last there are some other UIViews acting as pinch, the azure ones. I decided to use a further UIView as container so that the sub views are zoomed all together by scrolling. So, my zoom code is the following:

class PhotoViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIScrollViewDelegate {

  @IBOutlet weak var scrollView: UIScrollView!
  @IBOutlet weak var containerView: ContainerController!

  override func viewDidLoad() {
    ...
    self.scrollView.delegate = self
    self.scrollView.showsVerticalScrollIndicator = true
    self.scrollView.flashScrollIndicators()

    self.scrollView.minimumZoomScale = 1.0
    self.scrollView.maximumZoomScale = 10.0

  }

  func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return self.containerView
  }

}

Upvotes: 0

Views: 817

Answers (1)

Muhammad Waqas Bhati
Muhammad Waqas Bhati

Reputation: 2805

You have to override this method of Scrollview Delegate in your ViewController for both zooming and panning

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

   scale *= [[[self.scrollView window] screen] scale];
   [view setContentScaleFactor:scale]; // View which you want to Zoom & pan.
   for (UIView *subview in view.subviews) {
    [subview setContentScaleFactor:scale]; // all the Container Views
    }
 }

Hope it will help you to solve your problem.

Upvotes: 1

Related Questions