gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:23:29 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个可拖动的 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 作为容器,以便 subview 通过滚动一起缩放。所以,我的缩放代码如下:

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

希望能帮助您解决问题。

关于ios - 如何在 UIView 上使用 UIPanGestureRecognizer 时禁用滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34311001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com