图像缩放比例未重置

Image Zoom Scale is Not Resetting

所以我的视图控制器中有两个按钮(附有图像)。单击任一按钮时,该图像会居中弹出。

问题是,第一个按钮的图像在使用后不会重置其缩放比例和位置(第二个会重置)。所以当你第二次点击图片时,它仍然是放大的,并且没有对齐。

以下是仅用于缩放功能的代码:

//popup window
@IBOutlet var imageView1: UIView!
@IBOutlet var imageView2: UIView!
//scroll view
@IBOutlet weak var scrollView1: UIScrollView!
@IBOutlet weak var scrollView2: UIScrollView!
//image
@IBOutlet weak var zoomImageView1: UIImageView!
@IBOutlet weak var zoomImageView2: UIImageView!
//background is dimmed when the popup window is active
@IBOutlet weak var backgroundButton: UIButton!

var button1Pressed = false
var button2Pressed = false

override func viewDidLoad() {
    super.viewDidLoad()

    self.scrollView1.minimumZoomScale = 1.0
    self.scrollView1.maximumZoomScale = 6.0
    self.scrollView2.minimumZoomScale = 1.0
    self.scrollView2.maximumZoomScale = 6.0

}

//this might be the problem code, not sure how to fix it though
func viewForZooming(in scrollView: UIScrollView) -> UIView? {

    if button1Pressed == true {
        return self.zoomImageView1
    } else {
        return self.zoomImageView2
    }

}

//resizes zoomed image when orientation changes
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {

    if UIDevice.current.orientation.isLandscape{
        imageView1.center = self.view.center
        imageView2.center = self.view.center
        imageView1.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        imageView2.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        scrollView1.zoomScale = 1.0
        scrollView2.zoomScale = 1.0

    } else if UIDevice.current.orientation.isPortrait{
        imageView1.center = self.view.center
        imageView2.center = self.view.center
        imageView1.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        imageView2.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        scrollView1.zoomScale = 1.0
        scrollView2.zoomScale = 1.0
    }

}

//activates the 1st image
@IBAction func showImageView1(_ sender: Any) {

    animateIn1()
    button1Pressed = true

}

//activates the 2nd image
@IBAction func showImageView2(_ sender: Any) {

    animateIn2()
    button2Pressed = true

}

//closes either image
@IBAction func closeImageView(_ sender: Any) {

    animateOut()
    button1Pressed = false
    button2Pressed = false

}

func animateIn1() {

    self.scrollView1.zoomScale = 1.0

    self.view.addSubview(imageView1)
    imageView1.center = self.view.center
    imageView1.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
    imageView1.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
    imageView1.alpha = 0

    self.backgroundButton.alpha = 0.7

    UIView.animate(withDuration: 0.4) {
        self.imageView1.alpha = 1
        self.imageView1.transform = CGAffineTransform.identity
    }
}

func animateIn2() {

    self.scrollView2.zoomScale = 1.0

    self.view.addSubview(imageView2)
    imageView2.center = self.view.center
    imageView2.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
    imageView2.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
    imageView2.alpha = 0

    self.backgroundButton.alpha = 0.7

    UIView.animate(withDuration: 0.4) {
        self.imageView2.alpha = 1
        self.imageView2.transform = CGAffineTransform.identity
    }
}

func animateOut() {

    if button1Pressed == true {

        UIView.animate(withDuration: 0.3, animations: {
            self.imageView1.transform = CGAffineTransform(scaleX: 1, y: 1)
            self.imageView1.alpha = 0

            self.backgroundButton.alpha = 0

        }) { (success:Bool) in
            self.imageView1.removeFromSuperview()
        }

    } else if button2Pressed == true {

        UIView.animate(withDuration: 0.3, animations: {
            self.imageView2.transform = CGAffineTransform(scaleX: 1, y: 1)
            self.imageView2.alpha = 0

            self.backgroundButton.alpha = 0

        }) { (success:Bool) in
            self.imageView2.removeFromSuperview()
        }
    }
}

这可能很简单。

如有任何帮助,我们将不胜感激。

与其检查 button1Pressed == true,不如检查作为参数给出的滚动视图:

func viewForZooming(in scrollView: UIScrollView) -> UIView? {

    if scrollView == scrollView1 {
        return self.zoomImageView1
    } else {
        return self.zoomImageView2
    }

}