2017-08-28 78 views
0

所以我有兩個按鈕(圖像附加在它們)在我的視圖控制器。當點擊任何一個時,該圖像的中心彈出。圖像縮放比例沒有被重置

問題是,第1個按鈕的圖像在使用後沒有重置其縮放比例和位置(第二個按鈕)。所以當你第二次點擊圖片時,它仍然放大,並且沒有對齊。

這裏是唯一變焦功能的代碼:

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

不是檢查button1Pressed == true你倒是應該檢查哪些滾動視圖給出一個說法:

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

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

} 
+0

這似乎已經奏效。謝謝!不知道爲什麼我沒有想到這樣做。 –