2017-08-28 3 views
0

ビューコントローラに2つのボタン(画像が添付されています)があります。いずれかをクリックすると、その画像の中央にポップアップが表示されます。画像ズームスケールがリセットされない

問題は、1番目のボタンの画像は、使用後にズームスケールと位置がリセットされないということです(2番目の機能)。だから、2回目に画像をクリックすると、まだズームインされ、位置がずれる。ここで

はズーム機能のためのコードです:

//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をチェックするのではなく、引数として与えられたscrollview確認する必要があります:

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

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

} 
+0

働いているように見えました。ありがとう!なぜ私はそれをやっていると思っていなかったのか分かりません。 –

関連する問題