[英]How to get iOS 11 drag and drop working on the iPhone
I've been trying out the new iOS 11 drag and drop feature. 我一直在尝试新的iOS 11拖放功能。 It's great, but it works only on iPad.
这很棒,但它只适用于iPad。 Apple claims that it works also on the iPhone, but I can't get it working there?
苹果声称它也适用于iPhone,但我无法在那里工作? Is Apple's claim false, or am I doing something wrong?
Apple的说法是假的,还是我做错了什么?
You're installing a UIDragInteraction object on some view, right? 你正在某个视图上安装UIDragInteraction对象,对吗? Well, by default, that drag interaction's
isEnabled
property is false
on an iPhone (in accordance with the device-dependent value of the isEnabledByDefault
class property). 嗯,默认情况下,iPhone上的拖拽交互的
isEnabled
属性为false
(根据isEnabledByDefault
类属性的设备相关值)。
So to switch on drag and drop on the iPhone, just set the drag interaction's isEnabled
to true when you create it: 因此,要在iPhone上打开拖放功能,只需在创建时将拖动交互的
isEnabled
为true:
override func viewDidLoad() {
super.viewDidLoad()
let dragger = UIDragInteraction(delegate: self)
self.dragView.addInteraction(dragger)
dragger.isEnabled = true // for iPhone: presto, we've got drag and drop!
}
Similarly for a table view or collection view, as pointed out by the other answer, you would need to set its dragInteractionEnabled
to true
, as it too is false
by default on an iPhone. 类似地,对于表视图或集合视图,如另一个答案所指出的,您需要将其
dragInteractionEnabled
设置为true
,因为它在iPhone上默认为false
。
With Swift 4 and iOS 11, according to your needs, you can pick one of the following ways in order to solve your problem. 使用Swift 4和iOS 11,根据您的需要,您可以选择以下方法之一来解决您的问题。
UITableView
on iPhone UITableView
拖放交互 UITableView
has a property called dragInteractionEnabled
. UITableView
有一个名为dragInteractionEnabled
的属性。 dragInteractionEnabled
has the following declaration : dragInteractionEnabled
具有以下声明 :
var dragInteractionEnabled: Bool { get set }
A Boolean value indicating whether the table view supports drags and drops between apps.
一个布尔值,指示表视图是否支持应用之间的拖放。
The default value of this property is
true
on iPad andfalse
on iPhone.此属性的默认值在iPad上为
true
,在iPhone上为false
。 Changing the value totrue
on iPhone makes it possible to drag content from the table view to another app on iPhone and to receive content from other apps.在iPhone上将值更改为
true
可以将内容从表格视图拖动到iPhone上的其他应用程序,并从其他应用程序接收内容。
The following code shows how to use dragInteractionEnabled
in order to allow drag and drop interactions for UItableView
on iPhone: 以下代码显示了如何使用
dragInteractionEnabled
以允许iPhone上的UItableView
进行拖放交互:
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* ... */
tableView.dragInteractionEnabled = true
}
}
UICollectionView
on iPhone UICollectionView
交互 UICollectionView
has a property called dragInteractionEnabled
. UICollectionView
有一个名为dragInteractionEnabled
的属性。 dragInteractionEnabled
has the following declaration : dragInteractionEnabled
具有以下声明 :
var dragInteractionEnabled: Bool { get set }
A Boolean value indicating whether the collection view supports drags and drops between apps.
一个布尔值,指示集合视图是否支持应用程序之间的拖放。
The default value of this property is
true
on iPad andfalse
on iPhone.此属性的默认值在iPad上为
true
,在iPhone上为false
。 Changing the value totrue
on iPhone makes it possible to drag content from the collection view to another app on iPhone and to receive content from other apps.在iPhone上将值更改为
true
可以将内容从集合视图拖到iPhone上的其他应用程序,并从其他应用程序接收内容。
The following code shows how to use dragInteractionEnabled
in order to allow drag and drop interactions for UICollectionView
on iPhone: 以下代码显示如何使用
dragInteractionEnabled
以允许在iPhone上进行UICollectionView
拖放交互:
class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* ... */
collectionView?.dragInteractionEnabled = true
}
}
UIImageView
on iPhone UIImageView
交互 UIDragInteraction
has a property called isEnabled
. UIDragInteraction
有一个名为isEnabled
的属性。 isEnabled
has the following declaration : isEnabled
具有以下声明 :
var isEnabled: Bool { get set }
A Boolean value that specifies whether the drag interaction responds to touches and is allowed to participate in a drag activity.
一个布尔值,指定拖动交互是否响应触摸并允许参与拖动活动。
The following code shows how to use isEnabled
in order to allow drag interaction in addition to drop interaction for UIImageView
on iPhone: 以下代码显示了如何使用
isEnabled
以便除了在iPhone上的UIImageView
drop交互之外允许拖动交互:
class ViewController: UIViewController, UIDragInteractionDelegate, UIDropInteractionDelegate {
let imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(imageView)
imageView.image = UIImage(named: "MyImage")
imageView.isUserInteractionEnabled = true
imageView.contentMode = .scaleAspectFit
imageView.frame = view.bounds
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let dropInteraction = UIDropInteraction(delegate: self)
imageView.addInteraction(dropInteraction)
let dragInteraction = UIDragInteraction(delegate: self)
dragInteraction.isEnabled = true
imageView.addInteraction(dragInteraction)
}
/* ... */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.