HDAugmentedReality
iOS 上的增强现实组件。
描述
HDAugmentedReality 被设计用于大量静态 POIs 集中的区域,其主要目标是使所有 POIs 可见。这是通过垂直堆叠 POIs 来实现的,也就是说,更远的 POIs(一般会被较近的 POIs 遮挡)会被放置得更高。POIs 的高度被忽略。
功能
- 自动垂直堆叠注释视图
- 跟踪用户移动并更新可见注释
- 完全可定制的注释视图
- POI 平滑移动
- 在所有 iOS 设备(带有 GPS)上运行,并支持所有旋转
- 可以处理大量注释和屏幕上的注释视图
- 模拟器调试和地图控制器调试
- 具有地图和越界指示器的雷达视图
- 可按距离配置垂直偏移量
什么是下一步?
- 当设备针对ARAnnotationView时(聚焦模式),通知ARAnnotationView。这可以作为一种替代堆叠或与之一起使用的方法。类似的功能可以在Flightradar应用程序中看到。
Plist
- 添加NSLocationWhenInUseUsageDescription。这需要位置授权。
- 添加NSCameraUsageDescription。这需要摄像头授权。
- 可选:添加UIRequiredDeviceCapabilities(数组),并添加“陀螺仪”、“加速度计”、“GPS”和“位置服务”值。
CocoaPods
- 设置你的Podfile
platform :ios, '10.0'
use_frameworks!
target "TargetName" do
pod 'HDAugmentedReality', '~> 3.0'
end
基本用法
查看示例项目以获取完整示例。
导入
import HDAugmentedReality
创建注释。
let annotation1 = ARAnnotation(identifier: "bakery", title: "Bakery", location: CLLocation(latitude: 45.13432, longitude: 18.62095))
let annotation2 = ARAnnotation(identifier: "supermarket", title: "Supermarket", location: CLLocation(latitude: 45.84638, longitude: 18.84610))
let annotation3 = ARAnnotation(identifier: "home", title: "Home", location: CLLocation(latitude: 45.23432, longitude: 18.65436))
let dummyAnnotations = [annotation1, annotation2, annotation3].compactMap{ $0 }
创建ARViewController并配置ARPresenter。
// Creating ARViewController. You can use ARViewController(nibName:bundle:) if you have custom xib.
let arViewController = ARViewController()
// Presenter - handles visual presentation of annotations
let presenter = arViewController.presenter!
presenter.presenterTransform = ARPresenterStackTransform()
arViewController.dataSource = self
arViewController.setAnnotations(dummyAnnotations)
self.present(arViewController, animated: true, completion: nil)
实现ARDataSource并提供注释视图。这将针对每个注释调用。
func ar(_ arViewController: ARViewController, viewForAnnotation: ARAnnotation) -> ARAnnotationView
{
// Annotation views should be lightweight views, try to avoid xibs and autolayout all together.
let annotationView = TestAnnotationView()
annotationView.frame = CGRect(x: 0,y: 0,width: 150,height: 50)
return annotationView;
}
自定义
注释自定义
如果你有需要,可以子类化ARAnnotation并添加你的属性(参见示例项目中的TestAnnotation)。
AnnotationView 自定义/子类化
ARAnnotationView 仅为一个空视图,您应该子类化它并添加您的UI(标签、背景等)。尽量避免使用xibs和约束,因为它们会影响性能(请记住,屏幕上可以拥有数百个这样的视图)。例如,查看示例项目中的TestAnnotationView。
ARAnnotationView 有一个注解属性,它包含您创建的注解。因此,如果您子类化了ARAnnotation,则这些子类实例将被传递到ARAnnotationView中。
子类化
- 重写initialize方法来创建您的UI和初始化其他东西。
- 当用户距离或航向发生变化时,会调用bindUi方法。如果要更新这些信息,可以重写它。
ARViewController 自定义
自定义 XIB
您可以将 ARViewController.xib 复制到您的项目中,按需重命名和编辑,并通过 nibName 传给 ARViewController(nibName: "MyARViewController", bundle: nil)。
根据距离调整垂直偏移量。
let presenter = arViewController.presenter!
presenter.distanceOffsetMode = .manual
presenter.distanceOffsetMultiplier = 0.1 // Pixels per meter
presenter.distanceOffsetMinThreshold = 500 // Tell it to not raise annotations that are nearer than this
通过计数和距离限制显示的注解数量。
presenter.maxDistance = 5000 // Don't show annotations if they are farther than this
presenter.maxVisibleAnnotations = 100 // Max number of annotations on the screen
调整位置跟踪精度和航向/俯仰运动。
let trackingManager = arViewController.trackingManager
trackingManager.userDistanceFilter = 15 // How often are distances and azimuths recalculated (in meters)
trackingManager.reloadDistanceFilter = 50 // How often are new annotations fetched (in meters)
trackingManager.filterFactor = 0.4 // Smoothing out the movement of annotation views
trackingManager.minimumTimeBetweenLocationUpdates = 2 // Minimum time between location updates
雷达
您可以使用 MKMapView 和指示环添加雷达。请注意,添加雷达,特别是如果 radar.indicatorRingType 是 .precise,将会显著增加电池消耗。
let radar = RadarMapView()
radar.startMode = .centerUser(span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
radar.trackingMode = .centerUserWhenNearBorder(span: nil)
radar.indicatorRingType = .segmented(segmentColor: nil, userSegmentColor: nil)
radar.maxDistance = 5000 // Limit bcs it drains battery if lots of annotations (>200), especially if indicatorRingType is .precise
arViewController.addAccessory(radar, leading: 15, trailing: nil, top: nil, bottom: 15 + safeArea.bottom / 4, width: nil, height: 150)
自定义配件
您可以使用您的配件并与 ARViewController 一起使用。RadarMapView 是此类配件的示例。要制作附件,您必须实现 ARAccessory 协议(单方法)并在 ARViewController.addAccessory(...) 中调用它,或从 xib 通过 accessoriesOutlet 附加它。
自定义演示者
如果您不喜欢注解视图在屏幕上的显示或定位方式,您可以创建ARPresenter的子类并将其设置到ARViewController.presenter属性。
已知问题
- ARTrackingManager具有属性headingSource,默认设置为.coreLocation。这意味着航向将从CLLocationManager获取,而不如从CMDeviceMotion获取的那样平滑。如果将headingSource设置为.deviceMotion,它将使用CMDeviceMotion的航向,这将更平滑,但出于某种原因,当快速移动(例如在车内或公交车上)时,此值不准确。
- 当俯仰角>135°时,航向被反转/未补偿。这可以在原生的指南针应用程序中观察到:将iPhone平放在地上,屏幕指向天空(俯仰角=0),现在旋转iPhone围绕其x轴(轴链接以下)。一旦旋转超过135度,航向将跳变,这个跳变取决于您的设备方向。我修复了这个问题,但您需要设置ARTrackingManager.headingSource = .deviceMotion,这有其问题(上面已描述)。
组件
ARTrackingManager:用于跟踪和过滤ARViewController的内部使用类位置/航向/俯仰等。ARViewController将这些信息存储在ARViewController.arStatus对象中,然后传递给ARPresenter。此类不打算进行子类化。
ARPresenter:处理注释视图的创建并将它们在屏幕上布局。在执行任何操作之前,它首先通过距离和数量过滤注释以提高性能。此类还负责注释视图的垂直堆叠。如果需要自定义定位,例如,如果要根据高度定位注释,则需要子类化ARPresenter并覆盖xPositionForAnnotationView和yPositionForAnnotationView。
ARViewController:将所有这些结合起来。在ARPresenter上方呈现相机。从ARTrackingManager获取所有必要输入并将其传递给ARPresenter。
ARAnnotation:作为单个注释信息(位置、标题等)的来源。注释对象不提供注释的视觉表示。它与MKAnnotation类似。如果需要一些注释的附加信息,可以对其进行子类化。
ARAnnotationView:负责以视觉方式呈现注释。类似于MKAnnotationView。通常对其进行子类化以提供自定义外观。
ARStatus:结构体,包含屏幕(FOV)、设备(位置/航向/俯仰)以及屏幕上注释视图布局所需的所有其他重要信息。
版本历史
- 3.0.0:使用Swift 5编写,适用于iOS 10及更高版本
- 2.4.0:使用Swift 4.2编写,适用于iOS 8及更高版本
- 2.3.0:使用Swift 4.0编写,适用于iOS 8及更高版本
- 2.0.0:使用Swift 3.0编写,适用于iOS 8及更高版本
- 1.1.x:使用Swift 3.0编写,适用于iOS 8及更高版本
- 1.0.x:使用Swift 2.0编写,适用于iOS 7及更高版本
- 0.1.0:使用Swift 1.2编写,适用于iOS 7及更高版本
许可协议
HDAugmentedReality遵循MIT许可证发布。有关详细信息,请参阅LICENSE。