copy object and place it on the view

67 Views Asked by At

My swift code below when the code builds places a brown uiview on the view controller. What I would like it to do is when the user clicks on the copy button and then the brown box then re touches the view controller the object is pasted there. All my code is below and there is no storyboard. There is also a gif below with the copy feature.

enter image description here

import UIKit

class ViewController: UIViewController {



    var addBoxes = [UIImageView]()
    let subview = UIImageView()
    var currentView: UIView?

    var copyButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        copyButton.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(copyButton)

     
        copyButton.setTitle("Copy", for: .normal)
        
  
        NSLayoutConstraint.activate([

            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: 1),
            
          
        ])

   
        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)

        
        copyButton.backgroundColor = .blue
        copyButton.addTarget(self, action: #selector(copyObject), for: .touchDown)
    
       
    }
    
    
    


    @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 copyObject(){
        
    }

 

}
0

There are 0 best solutions below