Reputation: 411
So I have two buttons (which have images attached to them) in my view controller. When either is clicked there is a centered popup of that image.
The problem is, that the 1st button's image does not reset its zoom scale and position after use (the second does). So when you click on the image the second time, it is still zoomed in, and misaligned.
Here is the code for the zoom feature only:
//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()
}
}
}
It's probably something simple.
Any help would be greatly appreciated.
Upvotes: 0
Views: 270
Reputation: 14040
Instead of checking for button1Pressed == true
you should rather check which scrollview is given as an argument:
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
if scrollView == scrollView1 {
return self.zoomImageView1
} else {
return self.zoomImageView2
}
}
Upvotes: 1