react-native-navigation的迁移库

animations.mdx 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. ---
  2. id: docs-animations
  3. title: Animations
  4. sidebar_label: Animations
  5. ---
  6. import Tabs from '@theme/Tabs';
  7. import TabItem from '@theme/TabItem';
  8. import useBaseUrl from '@docusaurus/useBaseUrl';
  9. Various UI elements can be animated by declaring animation options.
  10. You can change the default animations for various commands, like push and pop, and even animate elements in
  11. your screens during screen transitions.
  12. #### Animation properties
  13. The following properties can be animated with our animation framework:
  14. * **x** - Translate the view to a coordinate along the x axis.
  15. * **y** - Translate the view to a coordinate along the y axis.
  16. * **translationX** - Translate the view along the x axis relative to its current x position.
  17. * **translationY** - Translate the view along the y axis relative to its current y position.
  18. * **alpha** - Apply alpha animation to the view. A value of 0 means the view is not visible, 1 means it's visible.
  19. * **scaleX** - Scale the view on the x axis. A value of 1 means that no scaling is applied.
  20. * **scaleY** - Scale the view on the y axis. A value of 1 means that no scaling is applied.
  21. * **rotationX** - Applies rotation around the x axis (in degrees, passed as a float).
  22. * **rotationY** - Applies rotation around the y axis (in degrees, passed as a float).
  23. * **rotation** - Rotates the view on all axis. Positive values result in clockwise rotation.
  24. ## Layout animations
  25. ### Stack animations
  26. When animating screens in and out of a stack, there are three elements you can work with:
  27. 1. **content** - screen being pushed or popped
  28. 2. **topBar** - stack's TopBar
  29. 3. **bottomTabs** - if stack is a child of a bottomTabs layout, we can control the `hide` and `show` animations of the bottomTabs.
  30. The following example demonstrates how to replace the default push and pop animations with slide animations.
  31. <Tabs
  32. defaultValue="push"
  33. values={[
  34. { label: 'Push', value: 'push', },
  35. { label: 'Pop', value: 'pop', },
  36. ]
  37. }>
  38. <TabItem value="push">
  39. ```js
  40. options: {
  41. animations: {
  42. push: {
  43. content: {
  44. translationX: {
  45. from: require('react-native').Dimensions.get('window').width,
  46. to: 0,
  47. duration: 300
  48. }
  49. }
  50. }
  51. }
  52. }
  53. ```
  54. </TabItem>
  55. <TabItem value="pop">
  56. ```js
  57. options: {
  58. animations: {
  59. pop: {
  60. content: {
  61. translationX: {
  62. from: 0,
  63. to: -require('react-native').Dimensions.get('window').width,
  64. duration: 300
  65. }
  66. }
  67. }
  68. }
  69. }
  70. ```
  71. </TabItem>
  72. </Tabs>
  73. ### Modal animations
  74. Modal animations are declared similarly to stack animations, only this time we animate the entire view and not only part of the UI (content).
  75. <Tabs
  76. defaultValue="show"
  77. values={[
  78. { label: 'Show', value: 'show', },
  79. { label: 'Dismiss', value: 'dismiss', },
  80. ]
  81. }>
  82. <TabItem value="show">
  83. The following example demonstrates how to show a modal with a fade-in animation.
  84. ```js
  85. options: {
  86. showModal: {
  87. alpha: {
  88. from: 0,
  89. to: 1,
  90. duration: 300
  91. }
  92. }
  93. }
  94. ```
  95. </TabItem>
  96. <TabItem value="dismiss">
  97. The following example demonstrates how to dismiss a modal with a fade-out animation.
  98. ```js
  99. options: {
  100. dismissModal: {
  101. alpha: {
  102. from: 1,
  103. to: 0,
  104. duration: 300
  105. }
  106. }
  107. }
  108. ```
  109. </TabItem>
  110. </Tabs>
  111. ## Shared element transitions
  112. Shared element transitions allow us to provide visual continuity when navigating between destinations. This also focuses user attention on a particular significant element, which then also gives such user better context when transitioning to some other destination.
  113. :::caution
  114. At the moment, the transition is available on iOS for push and pop while on Android it's available only for push commands. We are working on adding parity and expanding supported commands to show/dismiss modal and change BottomTabs.
  115. :::
  116. ### Transition breakdown
  117. Let's take a more in-depth look at the following example, which you can find in the playground app:
  118. > [Source screen](https://github.com/wix/react-native-navigation/blob/master/playground/src/screens/sharedElementTransition/CocktailsList.js) and the [Destination screen](https://github.com/wix/react-native-navigation/blob/master/playground/src/screens/sharedElementTransition/CocktailDetailsScreen.js)
  119. <p align="center">
  120. <img alt="Shared Element Transition" src={useBaseUrl('img/sharedElement.gif')} />
  121. </p>
  122. Four elements are animated in this example. Let's list the elements involved in the transition and note which properties are being animated.
  123. * **image** - the item image is animated to the next screen.
  124. * image scale (resizeMode)
  125. * position on screen
  126. * **image background** - each item has a "shadow" view which transitions to the next screen and turns into a colorful header.
  127. * scale
  128. * position on screen
  129. * color
  130. * **title** - the title of the item is animated to the next screen.
  131. * font size
  132. * font color
  133. * position on screen
  134. * **Description** - the description of the item in the destination screen.
  135. * fade-in
  136. * translation y
  137. ### Implementing shared element transitions
  138. #### Step 1 - set a nativeID prop to elements in the source screen
  139. In order for RNN to be able to detect the corresponding native views of the elements,
  140. each element must include a unique `nativeID` prop.
  141. ```jsx
  142. <Image
  143. source={item.image}
  144. nativeID={`image${item.id}`}
  145. style={styles.image}
  146. resizeMode={'contain'} />
  147. ```
  148. #### Step 2 - set a nativeID prop to elements in the destination screen
  149. ```jsx
  150. <Image
  151. source={this.props.image}
  152. nativeID={`image${this.props.id}Dest`}
  153. style={styles.image} />
  154. ```
  155. #### Step 3 - Declare the shared element animation when pushing the screen
  156. ```jsx
  157. Navigation.push(this.props.componentId, {
  158. component: {
  159. name: Screens.CocktailDetailsScreen,
  160. passProps: { ...item },
  161. options: {
  162. animations: {
  163. push: {
  164. sharedElementTransitions: [
  165. {
  166. fromId: `image${item.id}`,
  167. toId: `image${item.id}Dest`
  168. }
  169. ]
  170. }
  171. }
  172. }
  173. }
  174. });
  175. ```
  176. ## Element Transitions
  177. Element transitions also allow you to animate elements during shared element transitions.
  178. ### Recreating
  179. #### Step 1 - Set a nativeID prop to the element you'd like to animate
  180. An element can either be in the source or destination screen.
  181. ```jsx
  182. <Text
  183. nativeID='description'
  184. style={styles.description}>
  185. {this.props.description}
  186. </Text>
  187. ```
  188. #### Step 2 - Declare the element animation when pushing the screen
  189. ```jsx
  190. Navigation.push(this.props.componentId, {
  191. component: {
  192. name: Screens.CocktailDetailsScreen,
  193. passProps: { ...item },
  194. options: {
  195. animations: {
  196. push: {
  197. elementTransitions: [
  198. {
  199. id: 'description',
  200. alpha: {
  201. from: 0, // We don't declare 'to' value as that is the element's current alpha value, here we're essentially animating from 0 to 1
  202. duration: SHORT_DURATION
  203. },
  204. translationY: {
  205. from: 16, // Animate translationY from 16dp to 0dp
  206. duration: SHORT_DURATION
  207. }
  208. }
  209. ]
  210. }
  211. }
  212. }
  213. }
  214. });
  215. ```
  216. ## Peek and Pop (iOS 11.4+)
  217. react-native-navigation supports [Peek and pop](
  218. https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/Adopting3DTouchOniPhone/#//apple_ref/doc/uid/TP40016543-CH1-SW3) feature in iOS 11.4 and newer.
  219. This works by passing a ref to a component you want to transform into peek view. We have included a handy component to handle all the touches and ref for you:
  220. ```jsx
  221. const handlePress ({ reactTag }) => {
  222. Navigation.push(this.props.componentId, {
  223. component {
  224. name: 'previewed.screen',
  225. options: {
  226. preview: {
  227. reactTag,
  228. height: 300,
  229. width: 300,
  230. commit: true,
  231. actions: [{
  232. title: "Displayed Name",
  233. id: "actionId",
  234. style: 'default', /* or 'selected', 'destructive'*/
  235. actions: [/*define a submenu of actions with the same options*/]
  236. }]
  237. },
  238. },
  239. },
  240. });
  241. };
  242. const Button = (
  243. <Navigation.TouchablePreview
  244. touchableComponent={TouchableHighlight}
  245. onPress={handlePress}
  246. onPressIn={handlePress}
  247. >
  248. <Text>My button</Text>
  249. </Navigation.TouchablePreview>
  250. );
  251. ```
  252. All options except for reactTag are optional. Actions trigger the same event as [navigation button presses](https://wix.github.io/react-native-navigation/#/docs/topBar-buttons?id=handling-button-press-events). To react when a preview is committed, listen to the [previewCompleted](https://wix.github.io/react-native-navigation/#/docs/events?id=previewcompleted-ios-114-only) event.