图像缩放比例未重置

时间:2017-08-28 16:03:08

标签: ios swift image-zoom

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

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

以下是缩放功能的代码:

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

这可能很简单。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

而不是检查button1Pressed == true,而应该检查哪个scrollview作为参数:

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

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

}