Search code examples
iosswiftswift3nskeyedarchiveruilongpressgesturerecogni

How to take screenshot of a uiview and drag in swift?


i got the code but its not working, let me know if im did anything wrong. In the code i have a imageview which i created programmatically and adding the image into a uiview.Right now im unable to load the image in the "draggingView".

func longPressed(_ sender : UILongPressGestureRecognizer){

    let locationInView = sender.location(in: self.view)
    let cell = sender.view as? ParamterCollectionViewCell
    var tImageView: UIImageView!
    tImageView.frame = CGRect(x:0, y:0, width: 30 , height:30)
    tImageView.image = parameterImageArray[0]

    switch sender.state {
    case .began:
        // create an NSData object from myView
        let archive = NSKeyedArchiver.archivedData(withRootObject: tImageView!)

        // create a clone by unarchiving the NSData
       draggingView = NSKeyedUnarchiver.unarchiveObject(with: archive) as? UIView

        self.view.addSubview(draggingView!)
        draggingView!.center = locationInView;
        self.view.bringSubview(toFront: draggingView!)

        break

    case .changed:
        if (contentView.frame.contains(locationInView)) {
            draggingView?.center = locationInView;
        }
        break

    case .ended:
        break

    default:
        break

}

} }


Solution

  • Give this a try... Very similar in idea to your code, but I think it might be a bit better of an approach:

    func longPressed(_ sender : UILongPressGestureRecognizer) {
    
        let locationInView: CGPoint = sender.location(in: self.view)
        let locationInCollectionView: CGPoint = sender.location(in: self.collectionView)
    
        if sender.state == .began {
            if let indexPath: IndexPath = self.collectionView.indexPathForItem(at: locationInCollectionView) {
                if let cell = self.collectionView.cellForItem(at: indexPath) {
    
                    UIGraphicsBeginImageContext(cell.bounds.size)
                    cell.layer.render(in: UIGraphicsGetCurrentContext()!)
                    let cellImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
                    UIGraphicsEndImageContext()
    
                    self.movingCellImageView = UIImageView(image: cellImage)
                    self.movingCellImageView?.alpha = 0.75
    
                    self.view.addSubview(self.movingCellImageView!)
                    self.movingCellImageView?.center = locationInView
                }
            }
        }
        // Move movingCellImageView following finger position
        if sender.state == .changed {
            self.movingCellImageView?.center = locationInView
        }
        // Remove movingCellImageView when ended
        if sender.state == .ended {
            UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
                self.movingCellImageView?.alpha = 0
            }, completion: { (success: Bool) in
                self.movingCellImageView?.removeFromSuperview()
                self.movingCellImageView = nil
            })
        }
    }
    

    References:

    Tim Puckett - https://adoptioncurve.net/archives/2014/07/creating-a-draggable-uicollectionviewcell/

    Swift implementation by Chan Jing Hong http://eatliftswift.weebly.com/technical/creating-a-draggable-uicollectionviewcell