iOS 11.0 Chapter 6:Beginning Dray and Drop


Chapter 6:Beginning Drag and Drop

iOS 11.0 Chapter 6:Beginning Dray and Drop

iOS 11.0 Chapter 6:Beginning Dray and Drop

iOS 11.0 Chapter 6:Beginning Dray and Drop

One of the most anticipated announcements at WWDC ’17 was the introduction of Drag and Drop — a true game-changer for mobile devices. In typical Apple fashion, their engineers went above and beyond to deliver an interaction that feels as natural as it does intuitive.

一个最值得期待的公告在WWDC 17是拖放的引入移动设备的一个真正的游戏改变者。在典型的苹果时尚中,他们的工程师超越了它们,提供了一种与直觉一样自然的交互

If you haven’t yet experienced Drag and Drop in iOS 11 then I highly recommend putting down this book and having a play. If the concept of Drag and Drop interests you now — and it must, you are reading this chapter after all! — your enthusiasm will increase ten-fold once you’ve actually experienced it.

如果你还没有经历过iOS 11的拖放,那么我强烈建议你放下这本书并玩一玩。如果你现在对“拖放兴趣”这个概念有兴趣,那你一定在读这一章!一旦你真的体验到了,你的热情就会增加十倍。

For developers, the UIKit team provided two sets of APIs to integrate drag and drop in your apps. There are a standard set of APIs that provide for every aspect of the interaction, and then there are a set of higher-level abstractions built specifically for UICollectionView and UITableView.

对于开发者来说,UIKit团队提供两套API做综合拖放。有一套标准的API,提供各方面的互动,然后有一套专为更高层次的抽象建立特殊的对于UITableView UICollectionView。

In this chapter you’ll dive straight in and get your hands dirty with the latter set of APIs, by integrating drag and drop into an iPad bug-tracking app known as Bugray.

在这一章中你将直接潜入,把你的手弄脏的API后,通过拖放到iPad的应用程序称为bugray bug跟踪。

Getting started (入门)

The Razeware folks take their engineering efforts pretty seriously, and record each and every bug and feature request in Bugray, which breaks those items into three contexts: To Do, In Progress, and Done. Every item begins its life belonging to the To Do context, and moves through the other two contexts as it’s worked on.


However, productivity at Razeware has fallen off a cliff since introducing Bugray as there’s one massive flaw in the app — it’s not quite finished! You see, items can’t be moved between the different contexts by the user. It requires a developer to learn and understand the high-level Drag and Drop APIs in iOS 11, and then dive in and finish it.

然而,生产力在razeware已经跌下大坑自引入Bugray作为在应用程序-一个巨大的缺陷是没有完成!您知道,用户不能在不同上下文之间移动项目。它要求开发人员学习和理解iOS 11中的高级拖放API,然后跳入并完成它。

That developer is you.


Locate and open Bugray.xcodeproj with Xcode 9. When Xcode has finished launching,

select the iPad Pro (10.5-inch) simulator and hit Build and Run.

找出并打开Xcode 9的开放bugray.xcodeproj,当Xcode完成启动时。

选择iPad Pro(10.5英寸)模拟器并选中的Build和Run。

iOS 11.0 Chapter 6:Beginning Dray and Drop

Before jumping in and changing any code, take this brief tour through the existing files and structure of the app.


Open Main.storyboard and you’ll see everything’s been set up for you. The main

structure of Bugray looks like this:

Open Main.storyboard,你会看到一切都是为你准备的。主


iOS 11.0 Chapter 6:Beginning Dray and Drop

Bugray comprises just two view controllers:


  • The main container view controller embeds three instances of the smaller view controller to provide the look and feel of a kanban board.


  • The smaller view controller displays a list of all the items in a given context using a collection view. It also displays the number of items in that context in the upper-left corner.


These view controllers are implemented respectively by the following two UIViewController subclasses:


ContainerViewController: Provides each instance of BugListViewController with the context it’s responsible for displaying.


BugListViewController: Manages the collection view that displays the items in the current context.


Bug is the model object used throughout the app, whereas BugStore is a singleton that’s responsible for managing all the items in app. It uses Codable to load the items from a JSON file that’s part of the bundle, and provides methods to insert or remove items from the store.

整个BUG对象模型在项目中到处使用,然而 BugStore is a 单例是它负责管理应用程序中的所有项。它使用Codable 获取捆绑的JSON文件的条目,提供了插入或移除items 从store的方法。

Now that you’re familiar with Bugray and how it works, it’s time to knuckle down and add support for Drag and Drop!


Note: This chapter deals exclusively with the drag and drop APIs for UICollectionView, but Apple and the UIKit team have done a tremendous job providing consistency and parity between UICollectionView and UITableView. What you learn in this chapter should be directly transferable when working with table views.

注:本章专门处理拖放API UICollectionView,但是苹果和UIKit的团队已经做了大量的工作,并提供UICollectionView UITableView之间的一致性和奇偶校验。本章所学的内容在与表视图一起工作时可以直接传递。

Starting a drag

Configuring a UICollectionView to allow dragging is incredibly easy: You simply declare an object as conforming to the UICollectionViewDragDelegate protocol, implement a single method, and then set an instance of that object as the dragDelegate of the collection view. To keep things simple, you’ll update BugListViewController so it conforms to the protocol, and then set it as the drag delegate of the collection view.


To begin with, open BugListViewController.swift and add the following to the bottom of the file:


extension BugListViewController: UICollectionViewDragDelegate {

Here you’ve created an extension of BugListViewController that declares conformance to UICollectionViewDragDelegate. Using extensions in this way keeps your code uncluttered and well-organized.


Inside the extension, add the following method:


func collectionView(_ collectionView: UICollectionView,
  itemsForBeginning session: UIDragSession,
  at indexPath: IndexPath) -> [UIDragItem] {
// 1
  let item = UIDragItem(itemProvider: NSItemProvider())
  // 2
  return [item]

This is the only method that UICollectionViewDragDelegate requires you to implement. All others are optional. Inside the method you do the following two things:


  • Initialize an instance of UIDragItem with an empty NSItemProvider. Internal drag and drop — that is, between views within the same app — doesn’t require the use of NSItemProvider, hence why you’re passing an empty instance here.

1.初始化一个空的NSItemProvider UIDragItem实例。内部拖放,视图之间的在同一应用程序不需要NSItemProvider使用,因此你可以传递一个空的实例在这里。

You can find out more about inter-app drag and drop, and the role NSItemProvider

plays in that, in Jeff Rames’ Advanced Drag and Drop chapter later in this book.


在这本书中,以后在Jeff Rames关于Advanced Drag and Drop chapter。

  • Return an array of type UIDragItem, which in this case contains just the drag itemyou initialized in the previous step.



The final step to adopting dragging is to set the controller as the dragDelegate of the collection view.


Still in BugListViewController.swift, find viewDidLoad() and add the following statement to the very bottom:


collectionView.dragDelegate = self

That’s all there is to it! Build and run using the iPad Pro (10.5-inch) simulator, and

then tap-and-hold on any cell in any of the three collection views.

这就是它的全部!建立和运行使用iPad Pro(10.5英寸)模拟器,和然后点击并保存三个集合视图中的任何单元格。

iOS 11.0 Chapter 6:Beginning Dray and Drop

You’ll find that the tapped cell, after a short delay, will raise up and allow you to drag it around at will. However, lifting your finger — or mouse button — to drop the cell doesn’t actually result in a drop at that location, but instead the collection view simply animates the cell back to its origin.



PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » iOS 11.0 Chapter 6:Beginning Dray and Drop

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址