Fork me on GitHub
#cljsrn
<
2021-07-11
>
raspasov10:07:58

It was some time ago, but I think I spent a bunch of time trying to do PanResponder, but eventually ended up using react-native-gesture-handler which was better.

raspasov10:07:13

(more performant)

lepistane14:07:38

ok i've given up. I will claim it's impossible to make it work. I will @raspasov’s suggestion using rn-gesture-handler

lepistane18:07:57

@raspasov is there any way you could share working example using pan responder? I am having trouble understanding how to set any handler that has Animated.Event i am having trouble with even most minimalistic examples like this https://github.com/software-mansion/react-native-gesture-handler/blob/master/examples/Example/src/draggable/index.tsx

raspasov20:07:13

I’ll try to find it in a bit. It was some time ago. But I believe it was working.

2
lepistane22:07:49

alright here is the basic working example that works with X axis. Basically when u start moving the box it follows the finger after you let go it bounces to the center. Why i didn't come up with this earlier i am surprised myself. I was looking at RN examples all of those used Animated.event which does some bindings under the hood which doesn't play well with cljs. So the way i made it work is

(defn draggable-view []
  (let [bounce-value (new rn/Animated.Value 0)
        on-press #(do (prn "AAA")
                      (-> bounce-value
                          (rn/Animated.spring #js {:toValue 125
                                                   :friction 1
                                                   :useNativeDriver true})
                          (.start)))
        pan-responder (rn/PanResponder.create #js {:onStartShouldSetPanResponder (fn [arg] true)
                                                   :onPanResponderMove (fn [e state]
                                                                         (.setValue ^js bounce-value (goog-obj/get state "dx")))
                                                   :onPanResponderRelease (fn [e state]
                                                                            (-> bounce-value
                                                                                (rn/Animated.spring #js {:toValue 0
                                                                                                         :friction 1
                                                                                                         :useNativeDriver true})
                                                                                (.start)))})]
    (fn []

      [:> rn/Animated.View (merge (js->clj (.-panHandlers pan-responder))
                                  {:style {:background-color "red"
                                           :height 100
                                           :width 100
                                           :transform [{:translateX bounce-value}]}})])))
i think i already tried something like this before now but i couldn't make it work. This example was born from https://doc.ebichu.cc/react-native/releases/next/docs/animations.html#tracking-gestures that comment in following code section made it click. if anyone has better way of doing this i am all ears. hopefully someone will find it useful in the future 😄

👏 4
raspasov22:07:24

Glad it’s working! As for Animated.event:

(comment
 (def event (.-Animated.event rn/ReactNative))

 (some-scroll-view
  {:onScroll (event
              ;scroll-y-anim is a React Animated value
              (clj->js [{:nativeEvent {:contentOffset {:y scroll-y-anim}}}])
              (clj->js {:useNativeDriver true}))}))
Once you have that :onScroll handler, the scroll-y-anim Animated.value will be mutably updated as the scroll view moves across the y-axis.

raspasov22:07:17

onScroll={Animated.event( [{ nativeEvent: { contentOffset: { y: scrollYAnim } } }] )}