如何在UIView上使用UIPanGestureRecognizer时禁用滚动

时间:2015-12-16 11:35:51

标签: ios swift uigesturerecognizer uipangesturerecognizer

在我的应用中,UIView中包含一个可拖动的UIScrollView,以便我可以缩放它。此移动UIView有一个UIPanGestureRecognizer,但是如果我先放大然后我尝试移动UIView,那就不会发生,而是执行滚动,就好像未检测到UIPanGestureRecognizer一样

enter image description here

这是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()

}

正如您所看到的,我尝试在用户点按可拖动的UIView时尝试禁用滚动,并在结束时重新激活它,但它不起作用。我哪里弄错了?我错过了什么?

更新 所以更好地解释一下,我有一个UIScrollView,其中包含一个UIView,在这最后有一些其他UIViews充当捏,蔚蓝的。我决定使用另一个UIView作为容器,以便通过滚动将子视图一起缩放。所以,我的缩放代码如下:

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
  }

}

1 个答案:

答案 0 :(得分:1)

  

您必须在ViewController中覆盖Scrollview Delegate的这种方法,以进行缩放和平移

- (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
    }
 }

希望它能帮助您解决问题。