触觉反馈库 是一个简单的触觉反馈生成器。
$ pod try Haptica
需求
- iOS 9.0+
- Xcode 8.0+
- Swift 5 (Haptica 3.x), Swift 4 (Haptica 2.x), Swift 3 (Haptica 1.x)
触觉反馈需求
- 支持Taptic Engine的设备。
- 应用正在前台运行。
- 系统触觉反馈设置已启用。
使用
使用一种触觉反馈类型生成。
Haptic.impact(.light).generate()
反馈类型
- 冲击 : (UIImpactFeedbackStyle) - 使用冲击反馈生成器来表示发生冲击。例如,当用户界面对象发生碰撞或定位时,可以触发冲击反馈。
- 轻微
- 中等
- 沉重
- 通知:(UINotificationFeedbackType) - 使用通知反馈来传达任务或操作成功、失败或产生某种类型警告的信息。
- 成功
- 警告
- 错误
- 选择 - 使用选择反馈来传达一系列离散值之间的移动。
震动模式
播放自定义震动模式
Haptic.play("..oO-Oo..", delay: 0.1)
使用模式符号来表示自定义震动。
O
- 重击o
- 中击.
- 轻击X
- 坚硬击x
- 软击-
- 等待 0.1 秒
或者播放一组音符
Haptic.play([.haptic(.impact(.light)), .haptic(.impact(.heavy)), .wait(0.1), .haptic(.impact(.heavy)), .haptic(.impact(.light))])
UIButton 扩展
要启用按钮的触觉反馈,设置以下属性
isHaptic
- 启用触觉反馈hapticType
- 触觉反馈类型
button.isHaptic = true
button.hapticType = .impact(.light)
或者使用以下函数来设置控制事件的触觉反馈类型
addHaptic()
- 为控制事件添加触觉反馈removeHaptic()
- 移除控制事件的触觉反馈
button.addHaptic(.selection, forControlEvents: .touchDown)
button.removeHaptic(forControlEvents: .touchDown)
函数/属性
var isHaptic: Bool // enables haptic feedback
var hapticType: Haptic? // haptic feedback type
var hapticControlEvents: UIControl.Event? // haptic feedback control events
func addHaptic(_ haptic: Haptic, forControlEvents events: UIControl.Event) {} // add haptic feedback for control events
func removeHaptic(forControlEvents events: UIControl.Event) {} // remove haptic feedback for control events
音效
使用 Peep 将音效添加到 Haptica。
Peep.play(sound: KeyPress.tap)
安装
使用 CocoaPods
要使用 CocoaPods 安装,请简单地在您的 Podfile
中添加以下内容
use_frameworks!
pod "Haptica"
使用 Carthage 安装
要使用 Carthage 安装,请简单地在您的 Cartfile
中添加以下内容
github "efremidze/Haptica"
交流
- 如果您找到一个错误,请提交一个 issue。
- 如果您有功能请求,请提交一个 issue。
- 如果您想做出贡献,请提交一个 pull request。
提及
许可证
Haptica 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。