Creating Haptic Feedback in iOS 13 with Core Haptics

栏目: IT技术 · 发布时间: 4年前

内容简介:In previous iOS versions, developers were not able to create custom haptic feedback, andHaptic feedback is the use of advanced vibration patterns to carry information to the user. There are a lot of benefits to using haptic feedback in your app. It will pr

In previous iOS versions, developers were not able to create custom haptic feedback, and UINotificationFeedbackGenerator was the only way to use haptics in your app. But with iOS 13 release this changed, and Apple finally added a new framework called Core Haptics , which allows developers to define and play custom haptic feedback patterns.

What is Haptic Feedback?

Haptic feedback is the use of advanced vibration patterns to carry information to the user. There are a lot of benefits to using haptic feedback in your app. It will provide better UX, and users can interact with UI elements with a feedback similar to physical objects.

In this article, we will take a look at the configuration and usage of Core Haptics framework, as well as ways to describe patterns required for haptics playback.

Core Haptics Overview

To start working with the Core Haptics framework, you’ll first need to create a CHHapticEngine instance. CHHapticsEngine provides a sort of connection between your app and Taptic Engine hardware.

Haptic patterns can be composed by using CHHapticPattern class and nested CHHapticEvent objects. As an event type, CHHapticEvent allows you to use haptic touch or audio resources.

Creating Haptic Feedback in iOS 13 with Core Haptics

Haptic Events

Haptic Transient

Transient, as it describes itself, resembles a short impulse, and the indicated intensity determines its duration.

Haptic Continuous

Unlike transient, continuous feedback runs with the specified duration. The maximum duration of a continuous haptic event is 30 seconds.

Creating Haptic Feedback in iOS 13 with Core Haptics

Commonly used parameters for haptic events are sharpness and intensity. Both can be a value in the 0 to 1 interval. If you want to control your events more precisely, you can additionally define attack , decay , or release time parameters.

Audio Events

Audio Continuous

This event plays audio continuously depending on the duration parameter.

Audio Custom

Haptic pattern player plays audio file only once. In this its behaviour is similar to the transient event.

In order to create a custom audio event, you need to register your file at first. CHHapticEngine provides a registerAudioResource method, which returns an identifier if the audio file was registered correctly. This identifier will be used later when you create the CHHapticEvent instance. If you prefer to use the AHAP file format to define your patterns, you need to specify a relative path to an audio resource in the EventWaveformPath parameter.

For every audio event you can additionally specify volume , brightness , pan , and pitch .

Haptic Patterns

Basically, the haptic pattern is a set of related events. Optionally you can define dynamic parameters or parameter curves, which allows you to change feedback with time.

CHHapticPattern allows you to define patterns in three ways:

Dictionaries

Create a pattern by using a dictionary literal representing the haptic, e.g.:

Array of events (and parameters)

In this case, pattern events are represented as objects in your code. As you may have noticed, this example definition looks similar to the dictionary structure.

AHAP files

Apple Haptic and Audio Pattern file format mostly resembles a JSON file with a haptic pattern defined with key-value pairs.

Patterns events definition structure almost looks like dictionary one, but with some additions on the top level.

Example

Let’s create an example haptic pattern by using an audio file and haptic touches as an accompaniment for our tune. For simplicity purposes, I will use an array of events to generate a pattern.

First of all, we need to create a CHHapticEngine instance and register our audio file. As a sound for our pattern, I downloaded this tune from Freesound and edited it a bit to meet audio resource requirements.

I’m going to use two event templates with different intensity and sharpness. As a kick accompaniment I decided to set intensity to max and sharpness to a little more than half. And for rhythm sharpness is set to low, but intensity still remains near the maximum.

While listening to the melody, I decided to separate rhythm feedback into three sections separated by more intense events. So I created a method that generates an events combination from the desired relative time.

After that, you need to create a player for your pattern by calling the corresponding method of your CHHapticEngine instance.

Start your engine and player.

Now you can try to run the example on a real device. Don’t forget to check our


以上所述就是小编给大家介绍的《Creating Haptic Feedback in iOS 13 with Core Haptics》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构与算法

数据结构与算法

卓滋德克 / 陈曙晖 / 清华大学出版社 / 2003-4-1 / 69.00

本书是一本介绍数据结构与算法的优秀书籍。 本书系统介绍了C++面向对象程序设计、算法复杂度、链表、栈、队列、递归、树、图、排序和查找算法、散列技术、数据压缩算法、内存管理等内容;尤其对递归算法进行了深入剖析。在附录中详细介绍了大O符号与标准模板库:在大多数章中提供了相应的实例分析和程序设计作业。 本书适合作为计算机软件专业或其他相关专业的教科书。对于需要参加计算机考试,......一起来看看 《数据结构与算法》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具