Use struct to copy uiview

I want my swift code to copy the object which is a brown view. So the user hits the copy button then the user hits the object they want to copy and then they point to the area where they want to place the object. Basically the gif below is exactly what I am look at I don’t know if you would do something I have been told you would use a struct to copy the size. You can see the struct is called ViewModel.

v4m1C

import UIKit;import CoreData
    
class ViewController: UIViewController {
    
  var addBoxes = [UIImageView]()
  let subview = UIImageView()
  var currentView: UIView?
  var box = UIButton()
  var copyButton = UIButton()
        
  override func viewDidLoad() {
    super.viewDidLoad()
    [box,copyButton].forEach{
      $0.translatesAutoresizingMaskIntoConstraints = false
      view.addSubview($0)
    }
      
    box.backgroundColor = .red
    box.setTitle("Add", for: .normal)
    copyButton.setTitle("Copy", for: .normal)
    NSLayoutConstraint.activate([
      box.leadingAnchor.constraint(equalTo: copyButton.trailingAnchor),
      box.bottomAnchor.constraint(equalTo: view.bottomAnchor),
      box.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
      box.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
      copyButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      copyButton.bottomAnchor.constraint(equalTo: view.bottomAnchor),
      copyButton.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
      copyButton.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
    ])
    box.addTarget(self, action: #selector(addQuarter), for: .touchDown)
    copyButton.backgroundColor = .blue
  }
  
  override var prefersStatusBarHidden: Bool {
    return true
  }
    
  @objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {
    currentView = gesture.view
    let draggedView = gesture.view!
    view.bringSubviewToFront(draggedView)
            
    let translation = gesture.translation(in: view)
    draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
    gesture.setTranslation(.zero, in: view)
  }
  
  @objc func addQuarter(){
    let subview = UIImageView()
            
    subview.isUserInteractionEnabled = true
    addBoxes.append(subview)
    view.addSubview(subview)
            
    let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:)))
            
    subview.addGestureRecognizer(pan)
    subview.frame = CGRect(x: view.bounds.midX , y: view.bounds.midY + CGFloat(100), width: CGFloat(100), height: 100)
    subview.backgroundColor = .brown
    addBoxes.append(subview)
  }
     
  var selectModel: ViewModel?
    
  @objc func copyBox() { }
    
  func selectView(){
    // get current model
    selectModel = ViewModel(color: subview.backgroundColor!, size: subview.frame.size)
  }
    
    /*
      Logic Part:
    
      add an UITapGestureRecognizer  to the base view (add part view)
    
      in copy mode, the view isUserInteractionEnabled false,
    
      in add mode, the view isUserInteractionEnabled true,
    */

  @objc func AddBox() {
    subview.isUserInteractionEnabled = true
    // now in the add mode / paste mode
  }
    
    
  func touchToAdd(_ gesture: UITapGestureRecognizer){
    // get the tap location,
    // the location point is the new view's center
    //  use the model from `func selectView(){`
    //  now you got center . frame size 、color, enough to create a new one ( a copy)
    selectView()
  }
}

struct ViewModel {
  let color: UIColor
  let size: CGSize
}

This topic was automatically closed after 166 days. New replies are no longer allowed.