Aleem
Aleem

Reputation: 3291

How to enable Dragging in UITextfield in iOS 11

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

Answers (1)

Duyen-Hoa
Duyen-Hoa

Reputation: 15784

For iPhone, if the DnD is for the same screen, it's tricky:

  • Detect touch on the UITextField
  • Create a "visual copy" of this textfield if the user still touch the screen and move the finger.
  • Move this "visual copy" when the finger is moving
  • When user release the finger, detect the position to put this "visual copy"
  • Create the copy of uitextfield object, call addSubview to add to the right position
  • Adjust the autolayout constraint of this new uitextfield
  • Remove the old uitextfield + adjust the autolayout of the view that are related with it.

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

and https://developer.apple.com/documentation/uikit/drag_and_drop/adopting_drag_and_drop_in_a_custom_view

Upvotes: 2

Related Questions