I have 2 view controllers,in which both have UIView added and UIImageView added programatically.In ViewControllerA-The image is scaled, panned and rotated. I want to show the same image with same scaled, panned and rotated value in ViewControllerB.I tried adding CGAffineTransform to ViewControllerB, but the image is getting more zoomed.Please help me achieve the image in exact same scaled, panned and rotated value on View controller B.Thanks.
ViewControllerA -
class ViewControllerA: UIViewController {
var imageViewToTest = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
createCanvas()
}
@IBAction func backBtnCanvas(_ sender: UIButton) {
let VC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerB") as! ViewControllerB
VC.fetchImageViewToTest = imageViewToTest
let window = UIApplication.shared.windows.first
window?.rootViewController = VC
}
func createCanvas() {
let View1: UIView = {
let viewView = UIView()
viewView.translatesAutoresizingMaskIntoConstraints = false
viewView.contentMode = .scaleAspectFit
viewView.backgroundColor = .white
viewView.clipsToBounds = true
return viewView
}()
self.view.addSubview(View1)
View1.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
View1.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
View1.widthAnchor.constraint(equalTo: view..widthAnchor, constant: 0).isActive = true
View1.heightAnchor.constraint(equalTo: view..widthAnchor, multiplier: 1.0).isActive = true
let image_View1: UIImageView = {
let image_View1= UIImageView()
image_View1.image = image. // Add any image you have
image_View1.contentMode = .scaleAspectFill
image_View1.translatesAutoresizingMaskIntoConstraints = false
image_View1.clipsToBounds = true
return image_View1
}()
View1.addSubview(image_View1)
image_View1.topAnchor.constraint(equalTo: View1.topAnchor, constant: 0).isActive = true
image_View1.bottomAnchor.constraint(equalTo: View1.bottomAnchor, constant: 0).isActive = true
image_View1.leadingAnchor.constraint(equalTo: View1.leadingAnchor, constant: 0).isActive = true
image_View1.trailingAnchor.constraint(equalTo: View1.trailingAnchor, constant: 0).isActive = true
self.imageViewToTest = image_View
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
image_View1.isUserInteractionEnabled = true
image_View1.addGestureRecognizer(tapGestureRecognizer)
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction))
image_View1.addGestureRecognizer(pinchGesture)
let rotate = UIRotationGestureRecognizer(target: self, action: #selector(rotateAction))
image_View1.addGestureRecognizer(rotate)
if UserDefaults.standard.bool(forKey: "tapRecognizedForImage") == true {
createPanGestureRecognizer(targetView: image_View1)
}
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
UserDefaults.standard.set(true, forKey: "tapRecognizedForImage")
}
//Pan Gesture for Image
func createPanGestureRecognizer(targetView: UIImageView) {
let panGesture = UIPanGestureRecognizer(target: self, action:#selector(handlePanGesture))
targetView.addGestureRecognizer(panGesture)
}
@objc func handlePanGesture(panGesture: UIPanGestureRecognizer) {
let imageView = panGesture.view as! UIImageView
let translation = panGesture.translation(in: view)
panGesture.setTranslation(CGPoint.zero, in: view)
self.translationX = translation.x
self.translationY = translation.y
imageView.center = CGPoint(x: imageView.center.x+translation.x, y: imageView.center.y+translation.y)
imageView.isMultipleTouchEnabled = true
imageView.isUserInteractionEnabled = true
switch panGesture.state {
case .began,.ended: break
case .changed:
self.positionX = imageView.center.x
self.positionY = imageView.center.y
break
default:
break
}
}
ViewControllerB -
class ViewControllerB: UIViewController {
var fetchImageViewToTest = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
createCanvas()
}
func createCanvas() {
let View1: UIView = {
let viewView = UIView()
viewView.translatesAutoresizingMaskIntoConstraints = false
viewView.contentMode = .scaleAspectFit
viewView.backgroundColor = .white
viewView.clipsToBounds = true
return viewView
}()
self.view.addSubview(View1)
View1.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
View1.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
View1.widthAnchor.constraint(equalTo: view..widthAnchor, constant: 0).isActive = true
View1.heightAnchor.constraint(equalTo: view..widthAnchor, multiplier: 1.0).isActive = true
let image_View1: UIImageView = {
let image_View1= UIImageView()
image_View1.image = image. // Add any image you have
image_View1.contentMode = .scaleAspectFill
image_View1.translatesAutoresizingMaskIntoConstraints = false
image_View1.clipsToBounds = true
return image_View1
}()
View1.addSubview(image_View1)
image_View1.topAnchor.constraint(equalTo: View1.topAnchor, constant: 0).isActive = true
image_View1.bottomAnchor.constraint(equalTo: View1.bottomAnchor, constant: 0).isActive = true
image_View1.leadingAnchor.constraint(equalTo: View1.leadingAnchor, constant: 0).isActive = true
image_View1.trailingAnchor.constraint(equalTo: View1.trailingAnchor, constant: 0).isActive = true }
[![Screenshot of what I tried in my code for ViweControllerB][1]][1] [1]: https://i.stack.imgur.com/1c6cX.png
When you create the new
UIImageViewin your secondViewController set it'stransformequals tofirstImageView.transform, which is already rotated/scaled etc. You don't have to applyCGAffineTransformsto any other view insecondViewControllersince you originally add the transforms toUIImageViewonly.firstImageView.transformhas all the transformations you applied to it.I have added a sample Swift Playground code with the idea
Below is how it looks like in Playground when you run this. The yellow view has the
image_view1with transformations and green view creates a newimage_view2and use the transforms from theimage_view1