Reputation: 3291
I know its too early to ask about development of latest feature of iOS 11 Drag and Drop. This also says All drag and drop features are available on iPad. On iPhone, drag and drop is available only within an app.
So I'm looking to move textfield within single app in iPhone. I looked into UIDragInteraction
class but I don't know how to enable or active dragging feature in UITextfield
and I also noticed that .textDragDelegate
property added to UITextfield
but I don't know how to active dragging.
Looking for suggestion or help who already practiced this.
Thanks
Upvotes: 3
Views: 2435
Reputation: 15784
For iPhone, if the DnD is for the same screen, it's tricky:
For DnD between screen, technically, it's possible but the application must be designed for that requirement.
To enable dragging on an UIView object for iPad, you have to create a dragInteraction and add it to your UIViewObject. Then, you will have to enable the dropInteraction to your ViewController as well.
For example (not tested):
@IBOutlet weak var dragableTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//enable drag for your 'dragableTextField'
let dragInteraction = UIDragInteraction(delegate: self)
self.dragableTextField.addInteraction(dragInteraction)
//set up drop interaction delegate to this ViewController.
let dropInteraction = UIDropInteraction(delegate: self)
self.view.addInteraction(dropInteraction)
}
and implement those delegate
extension ViewController : UIDragInteractionDelegate {
//this is mandatory
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
//implement your code
}
}
extension ViewController : UIDropInteractionDelegate {
//this is optional
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
// If you want to enable drag-drop on UITextField object only:
return session.canLoadObjects(ofClass: [UITextField.self])
}
}
You need more works to load data or update UI at the destination target. You can read more at:
https://developer.apple.com/documentation/uikit/drag_and_drop/making_a_view_into_a_drop_destination
Upvotes: 2