Haptic Touch

Hi, i want to share this snippet that is a SLIGHTLY modified version of the Erel one.
Maybe it can be found easly with this specific thread name:


B4X:
Private FeedbackGenerator As NativeObject  'globals

FeedbackGenerator.Initialize("UIImpactFeedbackGenerator")

Sub Set_HapticTouch_Impact(Strenght As String) 'LIGHT, MEDIUM, HEAVY
    If FeedbackGenerator.IsInitialized Then
        Dim s As Int = 0
        Select Strenght
            Case "LIGHT"
                s = 0
            Case "MEDIUM"
                s = 1
            Case "HEAVY"
                s = 2
        End Select
        FeedbackGenerator = FeedbackGenerator.RunMethod("alloc", Null).RunMethod("initWithStyle:", Array(s))
    End If
End Sub

Call
B4X:
FeedbackGenerator.RunMethod("impactOccurred", Null)
to actually Start the vibration
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Almost.

Better like this:
B4X:
Private FeedbackGenerator As NativeObject  'globals

'Call CreateGenerator in AppStart.


' Strength - LIGHT, MEDIUM, HEAVY
Sub CreateGenerator (Strength As String)
   FeedbackGenerator.Initialize("UIImpactFeedbackGenerator")
    If FeedbackGenerator.IsInitialized Then
        Dim s As Int = 0
        Select Strength 
            Case "LIGHT"
                s = 0
            Case "MEDIUM"
                s = 1
            Case "HEAVY"
                s = 2
        End Select
        FeedbackGenerator = FeedbackGenerator.RunMethod("alloc", Null).RunMethod("initWithStyle:", Array(s))
    End If
End Sub

Sub Vibrate
 If FeedbackGenerator.IsInitialized Then
   FeedbackGenerator.RunMethod("impactOccurred", Null)
 End If
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…