在 Swift 中,如何以与缩放、平移和旋转完全相同的方式将缩放、平移和旋转图像从一个视图控制器添加到另一个视图控制器?

How to add a scaled, panned and rotated image from one view controller to another in exact same way as its scaled, panned and rotated, in Swift?

我有 2 个视图控制器,其中都添加了 UIView 和 UIImageView programatically.In ViewControllerA - 图像被缩放、平移和旋转。我想在 ViewControllerB.I 中显示具有相同缩放、平移和旋转值的相同图像尝试将 CGAffineTransform 添加到 ViewControllerB,但图像越来越多 zoomed.Please 帮助我实现完全相同缩放、平移和旋转的图像视图控制器上的旋转值 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 }

[![我在我的 ViweControllerB 代码中尝试的屏幕截图][1]][1] [1]: https://i.stack.imgur.com/1c6cX.png

当您在 secondViewController 中创建新的 UIImageView 集时,它的 transform 等于 firstImageView.transform,这已经是 rotated/scaled 等。您不必申请CGAffineTransformssecondViewController 中的任何其他视图,因为您最初仅将转换添加到 UIImageViewfirstImageView.transform 具有您对其应用的所有转换。

let image_View: UIImageView = {
    let imageV = UIImageView()
    imageV.image = image
    imageV.translatesAutoresizingMaskIntoConstraints = false
    imageV.contentMode = .scaleAspectFill
    imageV.clipsToBounds = true
    imageV.transform = firstImageView.transform
    return imageV
}()

我已经添加了一个示例 Swift 游乐场代码与想法

import UIKit
import PlaygroundSupport

let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 600))

let view1 = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 300))
view1.backgroundColor = .systemYellow

let image_view1: UIImageView = {
    let image_View1 = UIImageView()
    image_View1.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
    image_View1.image = UIImage(systemName: "trash.fill")
    image_View1.contentMode = .scaleAspectFill
   image_View1.clipsToBounds = true
    return image_View1
}()

//add transforms to the image_view1 (from panGestures, rotationGesture etc)
image_view1.transform = image_view1.transform.concatenating(CGAffineTransform(scaleX: 1.2, y: 1.2)).concatenating(CGAffineTransform(rotationAngle: 0.5))
    .concatenating(CGAffineTransform(translationX: 50, y: 50))

view1.addSubview(image_view1)


//create view2
let view2 = UIView(frame: CGRect(x: 0, y: 300, width: 500, height: 300))
view2.backgroundColor = .green

let image_view2: UIImageView = {
    let image_view2 = UIImageView()
    image_view2.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
    image_view2.image = UIImage(systemName: "trash.fill")
    //copy transforms from image_view1
    image_view2.transform = image_view1.transform
    image_view2.contentMode = .scaleAspectFill
   image_view2.clipsToBounds = true
    return image_view2
}()

view2.addSubview(image_view2)

containerView.addSubview(view1)
containerView.addSubview(view2)

PlaygroundPage.current.liveView = containerView

下面是您 运行 在 Playground 中的样子。黄色视图具有 image_view1 和转换,绿色视图创建一个新的 image_view2 并使用来自 image_view1

的转换