文章源地址:https://medium.com/swlh/10-code-snippets-from-wwdc20-5dba158e2903
作者: Francesco Marisaldi
翻译: Liaoworking
WWDC2020带给了我们很多新特性也宣布了很多令人激动的消息。这里有10个代码段开始在下一个iOS版本里面支持。每个都不超过5行。
1.SKOverlay
SKOverlay官方文档
第一个可以使我们在其他的app中显示一个浮层,来快速的下载应用。你可以设置位置和代理,你可以通过代理监听到出现,消失和处理对应的错误。
guard let scene = view.window?.windowScene else { return }
let config = SKOverlay.AppConfiguration(appIdentifier: "your-app-id", position: .bottom)
let overlay = SKOverlay(configuration: config)
overlay.present(in: scene)
它和 SKStoreProductViewController
的不同之处是它只是一个浮层而不是全屏幕展示。专为app clips设计。
用SKOverlay.AppClipConfiguration
来设置要响应的app和位置,图下图所示。
2.Configurations
Configurations是一个用于设置视图和Cell样式的全新API。很灵活,因为它可以用在任何的UIView上,包括collectionView和tableView的cell,使用简单。
下面就是一个UIListContentConfiguration
的使用例子:
var config = UIListContentConfiguration.subtitleCell()
config.image = UIImage(systemName:"tortoise")
config.text = "Hello world!"
config.secondaryText = "WWDC20"
let list = UIListContentView(configuration: config)
List content的设置有很多默认的设置,包括状态,内容和背景设置,此外,它替代了 UITableViewCell废弃的"textLabel","detailTextLabel","imageView"的属性,具体文档
3. CollectionView 新增的 Lists
从iOS 14开始,collectionView就可以设置类似于tableView的列表样式(这意味着tableView的时代要到了尽头),示例代码如下:
let config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
let layout = UICollectionViewCompositionalLayout.list(using: config)
列表会有不同的样式,而且会有滑动手势,分割线,accessories,WWDC的Lists in UICollectionView会告诉你更多细节。
4.定位精确度的改变。
Core Location框架这次也迎来了一些改变。可以允许用户选择分享给app的位置精确度的高或者低。
如果你需要高精读的定位,而用户分享给你的是低精度的怎么办?你可以用下面的代码来解决你的问题:
let manager = CLLocationManager()
manager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: "YOUR-PURPOSE-KEY") { (error) in
// Your code
}
暂时性的高精度只对运行中的进程可以,你必须要通过在info.plist中添加NSLocationTemporaryUsageDescriptionDictionary
key及对应的描述才可以。 如果有更多的需求,或者你感兴趣的话,可以通过WWDC中的What’s new in location和Design for location privacy两个session来了解。
5.行为追踪的授权
今年苹果对用户发隐私有很大的关注(其实最近几年年年都是...),不仅是定位和浏览器,而且应用的数据也会有限制。如果你获取设备的IDFA或者其他的敏感信息来追踪用户行为。你现在需要使用新的AppTrackingTrasparency
框架。
ATTrackingManager.requestTrackingAuthorization { (status) in
// your code
}
// To know current status
ATTrackingManager.trackingAuthorizationStatus
需要你在info.plist中去添加NSUserTrackingUsageDescription
key和对应的授权描述。用户可以在对于设置授权弹框不弹出。这样手机中所有app的这个授权弹框都不会弹出。Build trust through better privacy 这个session讲了更多相关的细节。
6.初始化UIControls可以有事件回调啦
UIcontrols可以通过闭包来传递事件了,就不需要之前的selectors来绑定方法。
如下:
let action = UIAction(title: "") { _ in print("Tapped!") }
let button = UIButton(frame: .zero, primaryAction: action)
7. UIBarButtonItem触发菜单栏
UIBarButtonItem点击可以触发显示菜单栏了。
苹果的用户交互指导建议多使用这样的方式。
使用代码如下
let newFolder = UIAction(title: "New Folder", image: UIImage(systemName: "folder.badge.plus")) { _ in print("NewFolder")}
let edit = UIAction(title: "Edit", image: UIImage(systemName: "pencil.circle")) { _ in print("Edit") }
let menu = UIMenu(title: "", children: [newFolder, edit])
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "ellipsis.circle"), menu: menu)
image
8.UIColorPickerViewController 颜色选择器
类似于图片选择器,其代理方法包括colorPickerViewControllerDidFinish(_:)
和colorPickerViewControllerDidSelectColor(_:)
使用如下:
let colorPicker = UIColorPickerViewController()
colorPicker.delegate = self
colorPicker.selectedColor = .orange
present(colorPicker, animated: true, completion: nil)
9.UIPageControl and UIDatePicker新api
分页小点点可以设置图片来作为page indicators。日期选择器有全新的UI,有弹出式菜单显示和.inline
样式
let pageControl = UIPageControl()
pageControl.preferredIndicatorImage = UIImage(systemName:"tortoise")
pageControl.setIndicatorImage(UIImage(systemName:"hare"), forPage:2)
let datePicker = UIDatePicker(frame: .zero)
datePicker.preferredDatePickerStyle = .inline
image
10.新增针对于Mac的userInterfaceIdiom
之前在设备判断UIDevice.current.userInterfaceIdiom
的时候只有.iPhone
, .iPad
两个选项,在xcode12中新添加了.mac
选项
无疑对多端开发更加友好。
网友评论