Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the maximum duration time of vibration in ios #98

Open
davidg327 opened this issue May 26, 2023 · 5 comments
Open

What is the maximum duration time of vibration in ios #98

davidg327 opened this issue May 26, 2023 · 5 comments

Comments

@davidg327
Copy link

Description

I am making an exercise app, and I have the haptic activated when the round starts, the problem is that a round lasts 50 seconds, if it passes 25 seconds it stops vibrating, this only happens on iOS, I would like to know if there is a restriction in the library about the maximum haptic time, this problem in android does not happen.

My example code

const options = {
enableVibrateFallback: false,
ignoreAndroidSystemSettings: true
};

const interval = useRef(null);

const hapticLight = () => {
trigger("impactLight", options)
}

useEffect(() => {
interval.current = setInterval(() => {
hapticLight()
}, 100);
return () => clearInterval(interval.current);
}, []);

Package version
"react": "17.0.2",
"react-native": "0.66.5",
"react-native-haptic-feedback": "^2.0.0",

Example visual
Captura de pantalla 2023-05-25 a la(s) 7 19 09 p m

@vanko0309
Copy link

I have the same issue, is there any limitation, or property that needs to be set?

@siliconivan
Copy link

A bit of a hack, but I had a similar issue and switched the trigger method to notificationSuccess and no longer had the issue. It felt similar enough for our use case.

@jpitkanen17
Copy link

jpitkanen17 commented Jul 25, 2024

I resolved this with the following patch, hope it's of any use.

react-native-haptic-feedback+2.2.0.patch

diff --git a/node_modules/react-native-haptic-feedback/ios/RNHapticFeedback/RNHapticFeedback.mm b/node_modules/react-native-haptic-feedback/ios/RNHapticFeedback/RNHapticFeedback.mm
index cf75baf..1ca2da3 100644
--- a/node_modules/react-native-haptic-feedback/ios/RNHapticFeedback/RNHapticFeedback.mm
+++ b/node_modules/react-native-haptic-feedback/ios/RNHapticFeedback/RNHapticFeedback.mm
@@ -100,11 +100,10 @@ RCT_EXPORT_METHOD(trigger:(NSString *)type options:(NSDictionary *)options)
         impactGeneratorMap = [[NSMutableDictionary alloc] init];
     if ([impactGeneratorMap objectForKey:key] == nil){
         [impactGeneratorMap setValue:[[UIImpactFeedbackGenerator alloc] initWithStyle:style] forKey:key];
-        [[impactGeneratorMap objectForKey:key] prepare];
     }
-    UIImpactFeedbackGenerator *generator = [impactGeneratorMap objectForKey:key];
-    [generator impactOccurred];
-    [generator prepare];
+    [[impactGeneratorMap objectForKey:key] prepare];
+    [[impactGeneratorMap objectForKey:key] performSelector:@selector(impactOccurred) withObject: nil afterDelay:0.0f];
+    [[impactGeneratorMap objectForKey:key] prepare];
 }
 
 -(void)generateNotificationFeedback:(UINotificationFeedbackType)notificationType{

@mkuczera
Copy link
Owner

mkuczera commented Aug 3, 2024

Is this related to a limitation of iOS regarding the haptics? Would be glad to have a reference, but couldn´t find anything. The diff is only triggering the prepare again, or am i missing something @jpitkanen17

@jpitkanen17
Copy link

Is this related to a limitation of iOS regarding the haptics? Would be glad to have a reference, but couldn´t find anything. The diff is only triggering the prepare again, or am i missing something @jpitkanen17

@mkuczera The extra prepare call might be redundant and something I tried before figuring the real solution out.

The actual solution is this call
[[impactGeneratorMap objectForKey:key] performSelector:@selector(impactOccurred) withObject: nil afterDelay:0.0f];

The key difference is the delay 0.0f which overrides the previous haptics call. I tested this with a slider that would stop vibrating on every click after a while and this fixed it. The only "bug" is that sometimes the you can feel the vibration being cancelled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants