DnDのは、同じ画面のためであれば、それはトリッキーです:
- ユーザーならば、このテキストフィールドの「視覚的なコピー」を作成するUITextField
- 上のタッチを検出画面に触れて指を動かす。
- 移動この「ビジュアル・コピー」ユーザーが指を離すと、指が
- を移動され、この「視覚的なコピーを」置く位置を検出
- 右に追加するaddSubviewを呼び出し、UITextFieldのオブジェクトのコピーを作成します。位置
- この新しいuitextフィールドの自動レイアウト制約を調整する
- 古いuitextfield +を削除して、それに関連するビューの自動レイアウトを調整します。画面間のドラッグ&ドロップのために
は、技術的に、それは可能ですが、アプリケーションは、その要件のために設計されなければなりません。
iPadのUIViewオブジェクトでドラッグできるようにするには、dragInteractionを作成してUIViewObjectに追加する必要があります。次に、ViewControllerへのdropInteractionも有効にする必要があります。例えば
(テストしていません):
@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)
}
し、データをロードするか、宛先ターゲットでUIを更新するために、より多くの作品を必要とするデリゲート
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])
}
}
を実装します。あなたはで詳細を読むことができます:
https://developer.apple.com/documentation/uikit/drag_and_drop/making_a_view_into_a_drop_destination
と https://developer.apple.com/documentation/uikit/drag_and_drop/adopting_drag_and_drop_in_a_custom_view
私はあなたの答えは、より多くのアプリケーション間でのiPadどこドラッグ&ドロップのnに関連すると思いますが、私は同じアプリ内でドラッグ&ドロップをしたいです。 – Aleem
ああ。私はその質問を理解できなかった。私は私の答えを更新します。画面間でドラッグすることを意味しますか?またはあなたのアプリの同じ画面内のDnDだけ? –