对于iPhone,如果免打扰是同一个画面,它的棘手:
- 检测触摸上的UITextField
- 如果用户创建该文本框的“视觉复制”仍然触摸屏幕并移动手指。
- 移动这个“视觉复制”当手指移动
- 当用户松开手指,发现把这种“视觉复制”
- 创建UITextField对象的副本的位置,请致电addSubview添加到右侧
- 调整这个新的uitextfield的自动布局约束
- 删除旧的uitextfield +调整与其相关的视图的自动布局。
对于屏幕之间的DnD,从技术上讲,这是可能的,但应用程序必须针对该需求进行设计。
要启用拖动为iPad的UIView的对象上,你必须创建一个dragInteraction并将其添加到您的UIViewObject。然后,你也必须启用dropInteraction到你的ViewController。
例如(未测试):
@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)
}
和执行这些代表
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])
}
}
你需要更多的工作在目的地目标加载数据或更新UI。你可以读到更多在:
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在应用程序之间拖动ñ下降,但我同样的应用程序中需要的DnD。 – Aleem
好的。我想知道这个问题。我会更新我的答案。你的意思是在屏幕之间拖动吗?或只是在你的应用程序的同一屏幕上的DnD? –