I've been trying out the new iOS 11 drag and drop feature. It's great, but it works only on iPad. Apple claims that it works also on the iPhone, but I can't get it working there? Is Apple's claim false, or am I doing something wrong?
我一直在尝试新的ios11拖放功能。它很棒,但只能在iPad上使用。苹果声称它也可以在iPhone上运行,但我不能让它在那里运行?苹果的说法是假的,还是我做错了什么?
2 个解决方案
#1
5
You're installing a UIDragInteraction object on some view, right? 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).
你在某个视图上安装了一个UIDragInteraction对象,对吧?默认情况下,拖动交互的isEnabled属性在iPhone上是假的(根据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。
#2
4
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和ios11,根据您的需要,您可以选择以下方法之一来解决您的问题。
#1. Allow drag and Drop interactions for UITableView
on iPhone
UITableView
has a property called dragInteractionEnabled
. dragInteractionEnabled
has the following declaration:
UITableView有一个属性叫做dragInteractionEnabled。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. 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.此属性的默认值在iPad上为true,在iPhone上为false。在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
}
}
#2. Allow drag and drop interactions for UICollectionView
on iPhone
UICollectionView
has a property called dragInteractionEnabled
. dragInteractionEnabled
has the following declaration:
UICollectionView有一个属性名为dragInteractionEnabled。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. 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.此属性的默认值在iPad上为true,在iPhone上为false。在iPhone上改变这个值,可以将内容从集合视图拖到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
}
}
#3. Allow drag and drop interactions for UIImageView
on iPhone
UIDragInteraction
has a property called isEnabled
. isEnabled
has the following declaration:
UIDragInteraction有一个属性叫isEnabled。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上的拖放交互:
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)
}
/* ... */
}
#1
5
You're installing a UIDragInteraction object on some view, right? 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).
你在某个视图上安装了一个UIDragInteraction对象,对吧?默认情况下,拖动交互的isEnabled属性在iPhone上是假的(根据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。
#2
4
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和ios11,根据您的需要,您可以选择以下方法之一来解决您的问题。
#1. Allow drag and Drop interactions for UITableView
on iPhone
UITableView
has a property called dragInteractionEnabled
. dragInteractionEnabled
has the following declaration:
UITableView有一个属性叫做dragInteractionEnabled。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. 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.此属性的默认值在iPad上为true,在iPhone上为false。在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
}
}
#2. Allow drag and drop interactions for UICollectionView
on iPhone
UICollectionView
has a property called dragInteractionEnabled
. dragInteractionEnabled
has the following declaration:
UICollectionView有一个属性名为dragInteractionEnabled。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. 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.此属性的默认值在iPad上为true,在iPhone上为false。在iPhone上改变这个值,可以将内容从集合视图拖到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
}
}
#3. Allow drag and drop interactions for UIImageView
on iPhone
UIDragInteraction
has a property called isEnabled
. isEnabled
has the following declaration:
UIDragInteraction有一个属性叫isEnabled。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上的拖放交互:
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)
}
/* ... */
}