diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-06-15 06:40:49 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-06-15 06:40:49 +0300 |
commit | e5e3b291ac0adc2eab0ec5840b7bad99517b4d59 (patch) | |
tree | e60947e9efa5a5f45b3b5d2b844cde18563c2aa6 /src/ios.m | |
parent | 4bcb7acb840627a197f8e9ed40256fc8ac0a01a9 (diff) |
iOS: Gentle haptic tap for sidebar-back-swipe
Diffstat (limited to 'src/ios.m')
-rw-r--r-- | src/ios.m | 43 |
1 files changed, 34 insertions, 9 deletions
@@ -66,6 +66,7 @@ API_AVAILABLE(ios(13.0)) | |||
66 | @interface HapticState : NSObject | 66 | @interface HapticState : NSObject |
67 | @property (nonatomic, strong) CHHapticEngine *engine; | 67 | @property (nonatomic, strong) CHHapticEngine *engine; |
68 | @property (nonatomic, strong) NSDictionary *tapDef; | 68 | @property (nonatomic, strong) NSDictionary *tapDef; |
69 | @property (nonatomic, strong) NSDictionary *gentleTapDef; | ||
69 | @end | 70 | @end |
70 | 71 | ||
71 | @implementation HapticState | 72 | @implementation HapticState |
@@ -107,26 +108,47 @@ API_AVAILABLE(ios(13.0)) | |||
107 | CHHapticPatternKeyEvent: @{ | 108 | CHHapticPatternKeyEvent: @{ |
108 | CHHapticPatternKeyEventType: CHHapticEventTypeHapticTransient, | 109 | CHHapticPatternKeyEventType: CHHapticEventTypeHapticTransient, |
109 | CHHapticPatternKeyTime: @0.0, | 110 | CHHapticPatternKeyTime: @0.0, |
110 | CHHapticPatternKeyEventDuration:@0.1 | 111 | CHHapticPatternKeyEventDuration:@0.1, |
112 | CHHapticPatternKeyEventParameters: @[ | ||
113 | @{ | ||
114 | CHHapticPatternKeyParameterID: CHHapticEventParameterIDHapticIntensity, | ||
115 | CHHapticPatternKeyParameterValue: @1.0 | ||
116 | } | ||
117 | ] | ||
111 | }, | 118 | }, |
112 | }, | 119 | }, |
113 | ], | 120 | ] |
121 | }; | ||
122 | self.gentleTapDef = @{ | ||
123 | CHHapticPatternKeyPattern: | ||
124 | @[ | ||
125 | @{ | ||
126 | CHHapticPatternKeyEvent: @{ | ||
127 | CHHapticPatternKeyEventType: CHHapticEventTypeHapticTransient, | ||
128 | CHHapticPatternKeyTime: @0.0, | ||
129 | CHHapticPatternKeyEventDuration:@0.1, | ||
130 | CHHapticPatternKeyEventParameters: @[ | ||
131 | @{ | ||
132 | CHHapticPatternKeyParameterID: CHHapticEventParameterIDHapticIntensity, | ||
133 | CHHapticPatternKeyParameterValue: @0.33 | ||
134 | } | ||
135 | ] | ||
136 | }, | ||
137 | }, | ||
138 | ] | ||
114 | }; | 139 | }; |
115 | } | 140 | } |
116 | 141 | ||
117 | -(void)playTapEffect { | 142 | |
143 | -(void)playHapticEffect:(NSDictionary *)def { | ||
118 | NSError *error = nil; | 144 | NSError *error = nil; |
119 | CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:self.tapDef | 145 | CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:def |
120 | error:&error]; | 146 | error:&error]; |
121 | // TODO: Check the error. | 147 | // TODO: Check the error. |
122 | id<CHHapticPatternPlayer> player = [self.engine createPlayerWithPattern:pattern error:&error]; | 148 | id<CHHapticPatternPlayer> player = [self.engine createPlayerWithPattern:pattern error:&error]; |
123 | // TODO: Check the error. | 149 | // TODO: Check the error. |
124 | [self.engine startWithCompletionHandler:^(NSError *err){ | 150 | [self.engine startWithCompletionHandler:^(NSError *err){ |
125 | if (err == nil) { | 151 | if (err == nil) { |
126 | /* Just keep it running. */ | ||
127 | // [self.engine notifyWhenPlayersFinished:^(NSError * _Nullable error) { | ||
128 | // return CHHapticEngineFinishedActionStopEngine; | ||
129 | // }]; | ||
130 | NSError *startError = nil; | 152 | NSError *startError = nil; |
131 | [player startAtTime:0.0 error:&startError]; | 153 | [player startAtTime:0.0 error:&startError]; |
132 | } | 154 | } |
@@ -341,7 +363,10 @@ void playHapticEffect_iOS(enum iHapticEffect effect) { | |||
341 | HapticState *hs = (HapticState *) appState_.haptic; | 363 | HapticState *hs = (HapticState *) appState_.haptic; |
342 | switch(effect) { | 364 | switch(effect) { |
343 | case tap_HapticEffect: | 365 | case tap_HapticEffect: |
344 | [hs playTapEffect]; | 366 | [hs playHapticEffect:hs.tapDef]; |
367 | break; | ||
368 | case gentleTap_HapticEffect: | ||
369 | [hs playHapticEffect:hs.gentleTapDef]; | ||
345 | break; | 370 | break; |
346 | } | 371 | } |
347 | } | 372 | } |