如何在 UIView 上使用 UIPanGestureRecognizer 时禁用滚动
How to disable scrolling while using UIPanGestureRecognizer on a UIView
在我的应用程序中,我有一个包含在 UIScrollView
中的可拖动 UIView
,以便我可以缩放它。这个移动 UIView
有一个 UIPanGestureRecognizer
但是如果我先放大然后我尝试移动 UIView
,那不会发生,而是执行滚动就像没有检测到 UIPanGestureRecognizer .
这是我关联到 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
}
}
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
}
}
希望能帮到您解决问题。
在我的应用程序中,我有一个包含在 UIScrollView
中的可拖动 UIView
,以便我可以缩放它。这个移动 UIView
有一个 UIPanGestureRecognizer
但是如果我先放大然后我尝试移动 UIView
,那不会发生,而是执行滚动就像没有检测到 UIPanGestureRecognizer .
这是我关联到 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
}
}
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
}
}
希望能帮到您解决问题。