I am working on a puzzle game where I have to drag the multiple UIviews
on another UIviews
or UIimages
in a Viewcontroller
.
Here is the link for the single uiview draggable.
In this program there is just a single UIview
which is draggable, Now i want to add more UIviews
to whom i can drag.
import UIKit
class GameViewController: UIViewController, UIGestureRecognizerDelegate, UITextFieldDelegate {
@IBOutlet weak var viewDrag: UIView!
var panGesture = UIPanGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
panGesture = UIPanGestureRecognizer(target: self, action: #selector(GameViewController.draggedView(_:)))
viewDrag.isUserInteractionEnabled = true
viewDrag.addGestureRecognizer(panGesture)
viewDrag.backgroundColor = UIColor.green
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func draggedView(_ sender:UIPanGestureRecognizer){
self.view.bringSubview(toFront: viewDrag)
let translation = sender.translation(in: self.view)
viewDrag.center = CGPoint(x: viewDrag.center.x + translation.x, y: viewDrag.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: self.view)
}
}
Here is the image of a white color square shape UIview
which is draggable on the UIimage
in viewcontroller