Some iPhones are equipped with a device called Taptic Engine. This is a device that creates the feel of pressing the home button instead of being a physical button. A click feeling occurs when you press and hold something other than the home button or when payment is completed.
I tried to operate this Taptic Engine using an application called Pyto.
iPhone SE (2020)(iOS13.4.1) Pyto(11.9)
I understand that Taptic Engine is a device that generates a click feeling, and that click feeling is called Haptic Feedback.
There are three types of Haptic Feedback on the iPhone. Here I referred to the site of [1]. Here It is also described in detail on the [2] site.
type | function |
---|---|
UIImpactFeedbackGenerator | React once. 3 levels of strength can be selected |
UISelectionFeedbackGenerator | For selection UI. The weakest reaction |
UINotificationFeedbackGenerator | For notification of success or failure. There are three types |
You can use ʻUIKit in Pyto. This is a function that allows you to touch Objective-C, like ʻobjc_util
in Pythonista. Actually, it is a library called rubicon-objc
, and it seems that Objective-C is operated using this. Pyto UIKit source code
It seems that you can minimize the delay by putting prepare ()
before calling, but it is optional. The feeling I tried didn't make a difference with or without it. prepare documentation
UIImpactFeedbackGenerator
You can adjust the strength in 3 steps. 0 is the weakest.
import UIKit
generator = UIKit.UIImpactFeedbackGenerator.alloc().init()
generator.prepare()
power = 0 # 0~3 stages of 2
generator.initWithStyle(power) #Set strength
generator.impactOccurred() #Activate
UISelectionFeedbackGenerator The strength cannot be adjusted.
import UIKit
generator = UIKit.UISelectionFeedbackGenerator.alloc().init()
generator.prepare()
generator.selectionChanged() #Activate
UINotificationFeedbackGenerator You can choose from 3 types of reactions. Success (ton ** ton ) = 0, Warning ( ton ton ) = 1, Error ( ton ton **) = 2.
import UIKit
generator = UIKit.UINotificationFeedbackGenerator.alloc().init()
generator.prepare()
mode = 0 # 0~3 types of 2.
generator.notificationOccurred(mode) #Activate
This is a code that tests all the strengths of the above three types at once.
import UIKit
import time
generator = UIKit.UIImpactFeedbackGenerator.alloc().init()
generator2 = UIKit.UISelectionFeedbackGenerator.alloc().init()
generator3 = UIKit.UINotificationFeedbackGenerator.alloc().init()
print("impact start")
generator.prepare()
for i in range(3):
print(f"-{i}")
for j in range(3):
generator.initWithStyle(i)
generator.impactOccurred()
time.sleep(0.3)
print("finished")
time.sleep(1)
print("selection start")
generator2.prepare()
for i in range(10):
generator2.selectionChanged()
time.sleep(0.1)
print("finished")
time.sleep(1)
print("notification start")
generator3.prepare()
for i in range(3):
print(f"-{i}")
generator3.notificationOccurred(i)
time.sleep(1)
print("finished")
It took me some time to realize that I should write .alloc (). Init ()
.
[1] https://qiita.com/WorldDownTown/items/2b5a72e41a95763727bb (How to use UIFeedbackGenerator and a convenient library) [2] https://note.com/tdksk/n/nb4498e59dcad (Effective micro-interaction with Haptic Feedback in iPhone app) [3] https://qiita.com/griffin_stewie/items/298f57ca3f1714ebe45c (I got the iPhone 7 so I immediately tried the Taptic Engine API) [4] https://developer.apple.com/documentation/uikit/uifeedbackgenerator?language=objc#2555399 (UIFeedbackGenerator) [5] https://rubicon-objc.readthedocs.io/en/latest/tutorial/tutorial-2.html (Rubicon Tutorial 2)
Recommended Posts