Search code examples
arraysswiftappendclonesubview

copy object from a subview then place object


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 like a clone of something like this.

enter image description here

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),
        
        
        ])
        
        copy.addTarget(self, action: #selector(copyBox), for: .touchDown)
        box.addTarget(self, action: #selector(addQuarter), for: .touchDown)
        
        
        copyButton.backgroundColor = .blue
        
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    @objc func copyBox() {
        
    }
    
    @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)
        
    }
}

Solution

  • Achieve your requirement with code

    class ViewController: UIViewController {
    
    var addBoxes = [UIImageView]()
    let subview = UIImageView()
    var currentView: UIView?
    var location: CGPoint = CGPoint.zero
    
    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),
        
        
        ])
        
        copyButton.addTarget(self, action: #selector(copyBox), for: .touchDown)
        box.addTarget(self, action: #selector(addQuarter), for: .touchDown)
        
        
        copyButton.backgroundColor = .blue
        
        self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:))))
        
    }
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    @objc func copyBox() {
        if let copyObject = currentView?.copyView as? UIImageView{
            copyObject.center = location
            copyObject.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:))))
            view.addSubview(copyObject)
        }
    }
    
    @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 handleTapGestured(_ gesture: UITapGestureRecognizer) {
        if self.view == gesture.view{
            let loc = gesture.location(in: self.view)
            location = loc
        }
    }
    
    @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)
        
    }
    }
    
    extension UIView{
    var copyView : UIView?{
        if let archiv = try? NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false){
            if let view = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archiv) as? UIView{
                return view
            }
        }
        return nil
    }
    }