如何让 iOS 11 在 iPhone 上进行拖放操作
How to get iOS 11 drag and drop working on the iPhone
我一直在尝试新的 iOS 11 拖放功能。这很棒,但它只适用于 iPad。 Apple 声称它也可以在 iPhone 上使用,但我不能让它在那里工作? Apple 的说法是错误的,还是我做错了什么?
您正在某个视图上安装 UIDragInteraction 对象,对吧?好吧,默认情况下,拖动交互的 isEnabled
属性 在 iPhone 上是 false
(根据 isEnabledByDefault
[=27 的设备相关值) =] 属性).
因此,要在 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!
}
类似地,对于 table 视图或集合视图,正如另一个答案所指出的,您需要将其 dragInteractionEnabled
设置为 true
,因为它也是 false
默认情况下 iPhone.
使用Swift 4和iOS 11,根据您的需要,您可以选择以下方法之一来解决您的问题。
#1。在 iPhone
上允许 UITableView
的拖放交互
UITableView
有一个 属性 叫做 dragInteractionEnabled
。 dragInteractionEnabled
有以下 declaration:
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 and false
on iPhone. Changing the value to true
on iPhone makes it possible to drag content from the table view to another app on iPhone and to receive content from other apps.
以下代码显示了如何使用 dragInteractionEnabled
以允许 iPhone 上 UItableView
的拖放交互:
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* ... */
tableView.dragInteractionEnabled = true
}
}
#2。允许 iPhone
上 UICollectionView
的拖放交互
UICollectionView
有一个 属性 叫做 dragInteractionEnabled
。 dragInteractionEnabled
有以下 declaration:
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 and false
on iPhone. Changing the value to true
on iPhone makes it possible to drag content from the collection view to another app on iPhone and to receive content from other apps.
以下代码显示了如何使用 dragInteractionEnabled
以允许 iPhone 上 UICollectionView
的拖放交互:
class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* ... */
collectionView?.dragInteractionEnabled = true
}
}
#3。允许 iPhone
上 UIImageView
的拖放交互
UIDragInteraction
有一个 属性 叫做 isEnabled
。 isEnabled
有以下 declaration:
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.
以下代码显示了如何使用 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)
}
/* ... */
}
我一直在尝试新的 iOS 11 拖放功能。这很棒,但它只适用于 iPad。 Apple 声称它也可以在 iPhone 上使用,但我不能让它在那里工作? Apple 的说法是错误的,还是我做错了什么?
您正在某个视图上安装 UIDragInteraction 对象,对吧?好吧,默认情况下,拖动交互的 isEnabled
属性 在 iPhone 上是 false
(根据 isEnabledByDefault
[=27 的设备相关值) =] 属性).
因此,要在 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!
}
类似地,对于 table 视图或集合视图,正如另一个答案所指出的,您需要将其 dragInteractionEnabled
设置为 true
,因为它也是 false
默认情况下 iPhone.
使用Swift 4和iOS 11,根据您的需要,您可以选择以下方法之一来解决您的问题。
#1。在 iPhone
上允许UITableView
的拖放交互
UITableView
有一个 属性 叫做 dragInteractionEnabled
。 dragInteractionEnabled
有以下 declaration:
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.
以下代码显示了如何使用 dragInteractionEnabled
以允许 iPhone 上 UItableView
的拖放交互:
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* ... */
tableView.dragInteractionEnabled = true
}
}
#2。允许 iPhone
上UICollectionView
的拖放交互
UICollectionView
有一个 属性 叫做 dragInteractionEnabled
。 dragInteractionEnabled
有以下 declaration:
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.
以下代码显示了如何使用 dragInteractionEnabled
以允许 iPhone 上 UICollectionView
的拖放交互:
class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* ... */
collectionView?.dragInteractionEnabled = true
}
}
#3。允许 iPhone
上UIImageView
的拖放交互
UIDragInteraction
有一个 属性 叫做 isEnabled
。 isEnabled
有以下 declaration:
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.
以下代码显示了如何使用 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)
}
/* ... */
}