Cocoa Touch 中的新特性

这些特性都是iOS11以后的一些特性,现在整理了一下,以备以后用到。

Drag Drop 新的交互方式 拖拽

Drag

需要 Drag 的对象要 add 一个 UIDragInteraction ,用法类似于 UIGestureRecognizer 。
UIDragInteraction 有一个 UIDragInteractionDelegate 的代理,可以提供数据,浮起的动画,和自定义的拖动时的动画。

Drop

接收 Drag 的对象的容器,容器要实现一个 UIDropInteractionDelegate 的代理,可以来根据拖入的对象调整当前的界面。

TableView, CollectionView, TextView, TextField, WebView 原生控件都支持

配合剪切板 UIPasteConfiguration 使用会更加方便。

这个功能手机上可以在 文件 这个系统 app 中体验,里面的 tableview 和 collectionview 的 item 都是支持拖拽的。

File Manager

UIDocumentBrowserViewController

多了一个文件管理的 VC,默认里面会显示当前 app 权限以内的文件,包括本地的和存在 iCloud 的【如果三方 app 在 info.plist 中声明了 UISupportsDocumentBrowser 或者 UIFileSharingEnabled 和 LSSupportsOpeningDocumentsInPlace 的话,可以获得第三方 app 的文件】。

构造方法如下【该构造还提供了文件筛选的能力】:

1
2
UIDocumentBrowserViewController* view  = [[UIDocumentBrowserViewController alloc]
initForOpeningFilesWithContentTypes:@[@".txt",@".pages",@".pdf"]];

值得注意的是官方文档中提到,在使用该 VC 时。

  1. 务必把文件管理里的 VC 作为应用的 根 VC,不要把它放在 navigation,tab 或者 split 视图中,也不要通过模态的样式展现出来。有需求的话用 UIDocumentPickerViewController 来替代。
    1. 由于文件可能被任何 app 中的 UIDocumentBrowserViewController 修改,所以对文件的操作尽量通过 UIDocument 子类 或者 NSFilePresenter 和 NSFileCoordinator 对象来操作。

新的 UINavigationBar

增加了大的标题栏【感觉不好看,而且会变卡,具体的去体验一下App Store里更新那个页面】,新增的 api,控制后面的 vc 层级是否需要继续展示大的 title,或者全部使用大的 title。可以在系统的 app 中体验这个特性。

1
2
3
4
//声明大标题
self.navigationController.navigationBar.prefersLargeTitles = YES;
//包含三种模式 aoto / always / never (后续 present 的view 是否需要大标题)
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAutomatic;

增加了统一的 navigation 搜索栏,不用再加在 tableview 的 header 中了。

1
self.navigationController.navigationItem.searchController

现在 navigation bar 是独立的,和你的 content vie 无关,为了更好的处理这些系统 view 的布局的变化,提供了

1
2
3
4
5
6
self.view.safeAreaLayoutGuide // auto layout @available(iOS 11.0, *)
self.view.safeAreaInsets // 代码布局 @available(iOS 11.0, *)

- (void)viewSafeAreaInsetsDidChange{
//当布局变化的时候的回调
}

UIScrollView

解决了 navigation vc 和 scrollview 的 contentInset 的冲突的问题,现在 navigation vc 不在修改 contentInset 了。
增加了 contentInsetAdjustmentBehavior 这个属性,默认为 UIScrollViewContentInsetAdjustmentAutomatic ,可以用来自定义滑动时候的便宜效果。
通过 adjustedContentInset 可以获取当前的偏移量。

TableView 的修改

滑动操作

Row 增加了 UISwipeActionsConfiguration ,用来处理滑动操作。
UISwipeActionsConfiguration 通过 UIContextualAction 数组来初始化,可以配置不同的动作,左滑右滑等,还能配置 UISwipeActionsConfiguration 的 performsFirstActionWithFullSwipe 【Bool】属性 来决定第一个事件是否响应完整的滑动。

新的分界线

可以配置相对值和绝对值的 Inset

1
self.tableView.separatorInsetReference = UITableViewSeparatorInsetFromAutomaticInsets;

密码自动填写功能

场景是在 web 上登录账户之后,点击下线 app ,然后调到 appstore,打开 app ,输入密码,这时候可以自动填写。【应该需要网页 / app 端的支持】
这里没有具体展开,有个专门介绍这个 session 【Introducing Password AutoFil】

Asset Catalogs 的调整

现在颜色也能放在 Asset Catalogs 中了,更方便的使用广色域的颜色。代码里可以通过下面的方法获取。

1
UIColor *color = [UIColor colorNamed:@"purple"];

屏幕刷新率

通过 UIScreen 的 maximumFramesPerSecond 可以获取当前屏幕的刷新率
获取屏幕的刷新率,增强滑动和动画效果。

文章目录
  1. 1. Drag Drop 新的交互方式 拖拽
    1. 1.1. Drag
    2. 1.2. Drop
    3. 1.3. TableView, CollectionView, TextView, TextField, WebView 原生控件都支持
  2. 2. File Manager
    1. 2.1. UIDocumentBrowserViewController
  3. 3. 新的 UINavigationBar
  4. 4. UIScrollView
  5. 5. TableView 的修改
    1. 5.1. 滑动操作
    2. 5.2. 新的分界线
  6. 6. 密码自动填写功能
  7. 7. Asset Catalogs 的调整
  8. 8. 屏幕刷新率
|