3

How to get half a millisecond vibration on ios in react native with expo? Similar to the vibration you get when typing on a keypad of a phone. I seem to get it on android with this Vibration.vibrate([0, 0, 0, 30]) but it does not replicate the same on ios

here is the code below

<TouchableOpacity onPress={() => Vibration.vibrate([0, 0, 0, 30])} style={{ marginTop: 100, alignItems: 'center' }}>
        <Text style={{ fontSize: 20 }}>Vibrate</Text>
</TouchableOpacity>
manny
  • 347
  • 5
  • 19
  • 2
    I *really* don't think you mean .5ms. Maybe 50ms, but based on your inputs, I'd guess 30ms. The term you're looking for is Haptic. https://stackoverflow.com/questions/43760502/enable-haptic-feedback-on-react-native-touchable-view – Joel Hager Jul 10 '21 at 20:24
  • 2
    Expo.Vibration doesnt offer changing vibration rate, use https://github.com/junina-de/react-native-haptic-feedback/ – Medet Tleukabiluly Jul 10 '21 at 20:39

2 Answers2

1

vibration in react-native is platform specific.

import { Vibration, Platform } from "react-native";

// vibrate is platform specific. default is 400ms
  const vibrate = () => {
    if (Platform.OS === "ios") {
      // this logic works in android too. you could omit the else statement
      const interval = setInterval(() => Vibration.vibrate(), 1000);
      // it will vibrate for 5 seconds
      setTimeout(() => clearInterval(interval), 5000);
    } else {
    
      Vibration.vibrate(5000);
    }
  };

Then use it as callback:

<TouchableOpacity onPress={vibrate} style={{ marginTop: 100, alignItems: 'center' }}>
        <Text style={{ fontSize: 20 }}>Vibrate</Text>
</TouchableOpacity>
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • I tried this for iOS. The vibration is not continous in this case. Is there a way to make it continous without stopping in the middle? – Zephyr Jun 16 '23 at 09:50
0

react-native-haptic-feedback makes the thing on iOS as mentioned by Medet Tleukabiluly here's the link: https://github.com/junina-de/react-native-haptic-feedback/ but unfortunately it didn't work on Android for me.

khandaniel
  • 306
  • 4
  • 13