-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Reanimated V2 for Switcher Animations
- Loading branch information
1 parent
fbf43a4
commit 21246dc
Showing
7 changed files
with
151 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,6 @@ | |
:ValueXY (fn []) | ||
:View {} | ||
:FlatList {} | ||
:Image {} | ||
:ScrollView {} | ||
:Text {}} | ||
:Easing {:bezier (fn []) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,75 @@ | ||
(ns status-im.switcher.animation | ||
(:require [quo.react-native :as rn] | ||
[reagent.core :as reagent] | ||
[status-im.switcher.constants :as constants] | ||
[status-im.ui.components.animation :as anim])) | ||
(:require [quo2.reanimated :as reanimated] | ||
[status-im.switcher.constants :as constants])) | ||
|
||
(def bottom-tabs-opacity (anim/create-value 1)) | ||
(def bottom-tabs-position (anim/create-value 0)) | ||
;; Component Animations | ||
(defn switcher-touchable-on-press-in | ||
[touchable-scale] | ||
(reanimated/animate-shared-value-with-timing touchable-scale 0.9 300 :easing1)) | ||
|
||
;; TODO(parvesh): Use 300, after using dispatch-later for opening card(otherwise pending animation issue) | ||
;; or OnAnimationEnd | ||
(def layout-animation #js {:duration 250 | ||
:create #js {:type (:ease-in-ease-out rn/layout-animation-types) | ||
:property (:scale-xy rn/layout-animation-properties)} | ||
:update #js {:type (:ease-in-ease-out rn/layout-animation-types) | ||
:property (:scale-xy rn/layout-animation-properties)} | ||
:delete #js {:type (:ease-in-ease-out rn/layout-animation-types) | ||
:property (:scale-xy rn/layout-animation-properties)}}) | ||
(defn switcher-touchable-on-press-out | ||
[switcher-opened? close-button-opacity switcher-button-opacity button-touchable-scale] | ||
(reanimated/animate-shared-value-with-timing button-touchable-scale 1 300 :easing2) | ||
(if @switcher-opened? | ||
(do | ||
(reanimated/animate-shared-value-with-timing close-button-opacity 0 300 :easing2) | ||
(reanimated/animate-shared-value-with-timing switcher-button-opacity 1 300 :easing1)) | ||
(do | ||
(reanimated/animate-shared-value-with-timing close-button-opacity 1 300 :easing1) | ||
(reanimated/animate-shared-value-with-timing switcher-button-opacity 0 300 :easing2))) | ||
(swap! switcher-opened? not)) | ||
|
||
(defn animate-layout [show? anim-values] | ||
(let [{:keys [width height]} (constants/dimensions) | ||
target-radius (- (max width height) | ||
constants/switcher-button-radius)] | ||
(rn/configure-next layout-animation) | ||
(reset! (:switcher-screen-radius anim-values) (if show? target-radius 1)) | ||
(reagent/flush))) | ||
|
||
(defn timing-animation [property toValue] | ||
(anim/timing property {:toValue toValue | ||
:duration 300 | ||
:useNativeDriver true})) | ||
|
||
(defn animate-components [show? view-id anim-values] | ||
(anim/start | ||
(anim/parallel | ||
(into | ||
[(timing-animation (:switcher-button-opacity anim-values) (if show? 0 1)) | ||
(timing-animation (:switcher-close-button-icon-opacity anim-values) (if show? 1 0)) | ||
(timing-animation (:switcher-close-button-background-opacity anim-values) (if show? 0.2 0))] | ||
(when (= view-id :home-stack) | ||
[(timing-animation bottom-tabs-opacity (if show? 0 1)) | ||
(timing-animation bottom-tabs-position (if show? (constants/bottom-tabs-height) 0))]))))) | ||
|
||
(defn animate [show? view-id anim-values] | ||
(reagent/flush) | ||
(animate-layout show? anim-values) | ||
(animate-components show? view-id anim-values)) | ||
;; Layout Animations | ||
(defn switcher-layout-animations [view-id] | ||
(let [{:keys [width height]} (constants/dimensions) | ||
half-width (/ width 2) | ||
switcher-bottom-position (constants/switcher-bottom-position view-id) | ||
switcher-target-radius (Math/hypot | ||
half-width | ||
(- height constants/switcher-button-radius switcher-bottom-position)) | ||
switcher-size (* 2 switcher-target-radius) | ||
switcher-from-x (- half-width constants/switcher-button-radius) | ||
switcher-from-y (- height switcher-bottom-position constants/switcher-button-size constants/switcher-height-offset) | ||
switcher-to-x (- half-width switcher-target-radius) | ||
switcher-to-y (- height constants/switcher-button-size switcher-bottom-position switcher-target-radius) | ||
switcher-entering-animation (.duration (new reanimated/key-frame | ||
(clj->js | ||
{:from {:width constants/switcher-button-size | ||
:height constants/switcher-button-size | ||
:originX switcher-from-x | ||
:originY switcher-from-y} | ||
:to {:width switcher-size | ||
:height switcher-size | ||
:originX switcher-to-x | ||
:originY switcher-to-y}})) 300) | ||
switcher-exiting-animation (.duration (new reanimated/key-frame | ||
(clj->js {:from {:width switcher-size | ||
:height switcher-size | ||
:originX switcher-to-x | ||
:originY switcher-to-y} | ||
:to {:width constants/switcher-button-size | ||
:height constants/switcher-button-size | ||
:originX switcher-from-x | ||
:originY switcher-from-y}})) 300) | ||
container-from-x (- switcher-from-x) | ||
container-from-y (- switcher-from-y) | ||
container-to-x (- switcher-to-x) | ||
container-to-y (- switcher-to-y) | ||
container-entering-animation (.duration (new reanimated/key-frame | ||
(clj->js {:from {:originX container-from-x | ||
:originY container-from-y | ||
:transform [{:scale 0.9}]} | ||
:to {:originX container-to-x | ||
:originY container-to-y | ||
:transform [{:scale 1}]}})) 300) | ||
container-exiting-animation (.duration (new reanimated/key-frame | ||
(clj->js {:from {:originX container-to-x | ||
:originY container-to-y | ||
:transform [{:scale 1}]} | ||
:to {:originX container-from-x | ||
:originY container-from-y | ||
:transform [{:scale 0.9}]}})) 300)] | ||
{:switcher-entering-animation switcher-entering-animation | ||
:switcher-exiting-animation switcher-exiting-animation | ||
:container-entering-animation container-entering-animation | ||
:container-exiting-animation container-exiting-animation})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,62 @@ | ||
(ns status-im.switcher.switcher | ||
(:require [quo.react-native :as rn] | ||
[reagent.core :as reagent] | ||
(:require [reagent.core :as reagent] | ||
[quo2.reanimated :as reanimated] | ||
[status-im.switcher.styles :as styles] | ||
[status-im.ui.components.animation :as anim] | ||
[status-im.switcher.animation :as animation] | ||
[status-im.ui.components.icons.icons :as icons] | ||
[status-im.react-native.resources :as resources] | ||
[status-im.switcher.switcher-container :as switcher-container])) | ||
|
||
(defn toggle-switcher-screen [switcher-opened? view-id anim-values] | ||
(swap! switcher-opened? not) | ||
(animation/animate @switcher-opened? view-id anim-values)) | ||
(defn switcher-button | ||
[view-id close-button-opacity switcher-button-opacity | ||
button-touchable-scale toggle-switcher-screen-fn] | ||
[:f> | ||
(fn [] | ||
(let [touchable-original-style (styles/switcher-button-touchable view-id) | ||
close-button-original-style (styles/switcher-close-button) | ||
switcher-button-original-style (styles/switcher-button) | ||
touchable-animated-style (reanimated/apply-animations-to-style | ||
{:transform [{:scale button-touchable-scale}]} | ||
touchable-original-style) | ||
close-button-animated-style (reanimated/apply-animations-to-style | ||
{:opacity close-button-opacity} | ||
close-button-original-style) | ||
switcher-button-animated-style (reanimated/apply-animations-to-style | ||
{:opacity switcher-button-opacity} | ||
switcher-button-original-style)] | ||
[reanimated/touchable-opacity {:active-opacity 1 | ||
:on-press-in #(animation/switcher-touchable-on-press-in | ||
button-touchable-scale) | ||
:on-press-out toggle-switcher-screen-fn | ||
:style touchable-animated-style} | ||
[reanimated/view {:style close-button-animated-style} | ||
[icons/icon :main-icons/close {:color :black}]] | ||
[reanimated/image {:source (resources/get-image :status-logo) | ||
:style switcher-button-animated-style}]]))]) | ||
|
||
(defn switcher-button [switcher-opened? view-id anim-values] | ||
[rn/touchable-opacity {:active-opacity 1 | ||
:on-press #(toggle-switcher-screen switcher-opened? view-id anim-values) | ||
:style (styles/switcher-button-touchable view-id)} | ||
[rn/animated-view {:style (styles/switcher-close-button-background | ||
(:switcher-close-button-background-opacity anim-values))}] | ||
[rn/animated-view {:style (styles/switcher-close-button-icon | ||
(:switcher-close-button-icon-opacity anim-values))} | ||
[icons/icon :main-icons/close {:color :white}]] | ||
[rn/animated-image-view {:source (resources/get-image :status-logo) | ||
:style (styles/switcher-button | ||
(:switcher-button-opacity anim-values))}]]) | ||
|
||
(defn switcher-screen [switcher-opened? view-id anim-values] | ||
[rn/view {:style (styles/switcher-screen | ||
view-id @(:switcher-screen-radius anim-values)) | ||
:pointer-events (if switcher-opened? :auto :none)} | ||
[switcher-container/container | ||
view-id @(:switcher-screen-radius anim-values) | ||
#(toggle-switcher-screen switcher-opened? view-id anim-values)]]) | ||
(defn switcher-screen [switcher-opened? view-id toggle-switcher-screen-fn] | ||
(let [{:keys [switcher-entering-animation switcher-exiting-animation | ||
container-entering-animation container-exiting-animation]} | ||
(animation/switcher-layout-animations view-id)] | ||
(when @switcher-opened? | ||
[reanimated/view {:entering switcher-entering-animation | ||
:exiting switcher-exiting-animation | ||
:style (styles/switcher-screen)} | ||
[reanimated/view {:entering container-entering-animation | ||
:exiting container-exiting-animation | ||
:style (styles/switcher-screen-container)} | ||
[switcher-container/tabs toggle-switcher-screen-fn]]]))) | ||
|
||
(defn switcher [view-id] | ||
(let [switcher-opened? (reagent/atom false) | ||
anim-values {:switcher-button-opacity (anim/create-value 1) | ||
:switcher-close-button-icon-opacity (anim/create-value 0) | ||
:switcher-close-button-background-opacity (anim/create-value 0) | ||
:switcher-screen-radius (reagent/atom 1)}] | ||
[:<> | ||
[switcher-screen switcher-opened? view-id anim-values] | ||
[switcher-button switcher-opened? view-id anim-values]])) | ||
[:f> | ||
(fn [] | ||
(let [switcher-opened? (reagent/atom false) | ||
close-button-opacity (reanimated/use-shared-value 0) | ||
switcher-button-opacity (reanimated/use-shared-value 1) | ||
button-touchable-scale (reanimated/use-shared-value 1) | ||
toggle-switcher-screen-fn #(animation/switcher-touchable-on-press-out | ||
switcher-opened? close-button-opacity switcher-button-opacity button-touchable-scale)] | ||
[:<> | ||
[switcher-screen switcher-opened? view-id toggle-switcher-screen-fn] | ||
[switcher-button view-id close-button-opacity switcher-button-opacity | ||
button-touchable-scale toggle-switcher-screen-fn]]))]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters