简体   繁体   English

将 UIPanGestureRecognizer 限制为边界 swift

[英]Limit UIPanGestureRecognizer to boundaries swift

I have a images inside uicollectionview cell which scroll horizontally, I want to achieve a feature like the facebook and photo app apple, you click on image and it covers the whole screen.我在 uicollectionview 单元格中有一个水平滚动的图像,我想实现像 facebook 和照片应用程序苹果这样的功能,您单击图像,它会覆盖整个屏幕。 You can pinch and pan the image, I want to add certain limitations same like the facebook and photo app, like when you pinch the picture you can pan maximum to its width.您可以捏合和平移图像,我想添加一些限制,例如 facebook 和照片应用程序,例如当您捏合图片时,您可以最大平移到其宽度。

I want the image to recenter again if user try to move image out of the boundaries.如果用户尝试将图像移出边界,我希望图像再次居中。 I am adding some screenshots to give idea about it.我正在添加一些屏幕截图以提供有关它的想法。

Right now I am using the simple code.现在我正在使用简单的代码。

 guard gestureRecognizer.view != nil else {return}
    print(self.imgView.frame)
    if self.imgView.frame.size.width < (self.imgOrignal.width+100) {
        return
    }

    let piece = gestureRecognizer.view!
    // Get the changes in the X and Y directions relative to
    // the superview's coordinate space.
    let translation = gestureRecognizer.translation(in: piece.superview)
    if gestureRecognizer.state == .began {
        self.initialCenter = piece.center
    }
    // Update the position for the .began, .changed, and .ended states
    if gestureRecognizer.state != .cancelled {
        // Add the X and Y translation to the view's original position.
        let newCenter = CGPoint(x: initialCenter.x + translation.x, y: initialCenter.y + translation.y)
        piece.center = newCenter
    }
    else {
        // On cancellation, return the piece to its original location.
        piece.center = initialCenter
    }
}

在此处输入图像描述 氖

I have resolved this issue by using UIScrollView zoom in and out, instead of using pinch and pan gesture, If any one wants to implement that functionality i am adding my code below.我已经通过使用 UIScrollView 放大和缩小解决了这个问题,而不是使用捏和平移手势,如果有人想要实现该功能,我将在下面添加我的代码。

  func setZoomScale() {
    let widthScale = self.bgView.frame.size.width / self.imgView.bounds.width
    let heightScale = self.bgView.frame.size.height / self.imgView.bounds.height
    let minScale = min(widthScale, heightScale)
    self.imgScrollView.minimumZoomScale = minScale
    self.imgScrollView.zoomScale = minScale
}


extension YOUR CLASS: UIScrollViewDelegate {

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return self.imgView
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    let imageViewSize = self.imgView.frame.size
    let scrollViewSize = scrollView.bounds.size
    let verticalInset = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
    let horizontalInset = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
    scrollView.contentInset = UIEdgeInsets(top: verticalInset, left: horizontalInset, bottom: verticalInset, right: horizontalInset)
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM