问题描述
我目前正在使用iOS 11中提供的 UIDragInteraction
和 UIDropInteraction
来提供简单的拖放功能,
我意识到一个不直观的元素是 UIDragInteraction
需要长按至少一秒钟才能起作用。我想知道是否有一种方法可以缩短长按时间??似乎并未强调这一点。
谢谢!
以下粘贴的实现以供参考:
class ViewController:UIViewController {
@IBOutlet var imageView:UIImageView!
@IBOutlet var dropArea:UIImageView!
重写func viewDidLoad(){
super.viewDidLoad()
let dragInteraction = UIDragInteraction(委托:自我)
imageView.addInteraction(dragInteraction)
dragInteraction.isEnabled = true
let dropInteraction = UIDropInteraction(代表:自我)
dropArea.addInteraction(dropInteraction)
}
}
扩展ViewController:UIDragInteractionDelegate {
func dragInteraction(_交互:UIDragInteraction,itemsForBeginning会话:UIDragSession)-> [UIDragItem] {
保护let image = imageView.image
else {return []}
let itemProvider = NSItemProvider(object:image)
return [UIDragItem( itemProvider:itemProvider)]
}
}
扩展ViewController:UIDropInteractionDelegate {
func dropInteraction(_交互:UIDropInteraction,sessionDidUpdate会话:UIDropSession)-> UIDropProposal {
return UIDropProposal(operation:.copy)
}
func dropInteraction(_交互作用:UIDropInteraction,performDrop会话:UIDropSession){
保护对象让itemProvider =会话.items.first?.itemProvider,
itemProvider.canLoadObject(ofClass:UIImage.self)
else {return}
itemProvider.loadObject(ofClass:UIImage.self){[弱自我] loadedItem,
防护中的错误让image = loadedItem为? UIImage
else {return}
DispatchQueue.main.async {
self?.dropArea.image = image
}
}
}
}
没有明显的方法这,但是我正面临着同样的问题,并偷看了dragInteraction附加到的视图的手势识别器。它是 _UIDragLiftGestureRecognizer
,它不是公共API的一部分,但事实证明这只是 UILongPressGestureRecognizer
的子类。 / p>
因此,在将 UIDragInteraction
添加到视图后,以及将该视图添加到视图层次结构之后(自我使用的是自定义UIView子类,我刚刚将其添加到 didMoveToSuperview()
)中,您可以执行以下操作:
如果让longPressRecognizer = poseRecognizers?.compactMap({$ 0 as?UILongPressGestureRecognizer})。first {
longPressRecognizer.minimumPressDuration = 0.1 //您的自定义值
}
I am currently using UIDragInteraction
and UIDropInteraction
made available in iOS 11 to make a simple drag and drop feature, where user could drag an UIImageView onto a UIView.
I realized that one unintuitive element to this is that the UIDragInteraction
requires a long press of at least a second to work. I was wondering if there is a way to shorten the long press duration? The docs on Apple doesn't seem to highlight this.
Thanks!
Implementation pasted below for reference:
class ViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
@IBOutlet var dropArea: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let dragInteraction = UIDragInteraction(delegate: self)
imageView.addInteraction(dragInteraction)
dragInteraction.isEnabled = true
let dropInteraction = UIDropInteraction(delegate: self)
dropArea.addInteraction(dropInteraction)
}
}
extension ViewController: UIDragInteractionDelegate {
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
guard let image = imageView.image
else { return [] }
let itemProvider = NSItemProvider(object: image)
return [UIDragItem(itemProvider: itemProvider)]
}
}
extension ViewController: UIDropInteractionDelegate {
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
return UIDropProposal(operation: .copy)
}
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
guard let itemProvider = session.items.first?.itemProvider,
itemProvider.canLoadObject(ofClass: UIImage.self)
else { return }
itemProvider.loadObject(ofClass: UIImage.self) { [weak self] loadedItem, error in
guard let image = loadedItem as? UIImage
else { return }
DispatchQueue.main.async {
self?.dropArea.image = image
}
}
}
}
There's no obvious way to do this, but I was just facing the same problem and took a peek into the gesture recognizers of the view that the dragInteraction is attached to. It a _UIDragLiftGestureRecognizer
which is not part of the public API, but turns out this is just a subclass of UILongPressGestureRecognizer
.
So, after having added your UIDragInteraction
to your view, and after having added that view to the view hierachy (since I'm using a custom UIView subclass I just added it into didMoveToSuperview()
), you can do something like this:
if let longPressRecognizer = gestureRecognizers?.compactMap({ $0 as? UILongPressGestureRecognizer}).first {
longPressRecognizer.minimumPressDuration = 0.1 // your custom value
}
这篇关于能够缩短UIDragInteraction的长按时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!