react-native-navigation的迁移库

style-animations.mdx 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. ---
  2. id: style-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. animations: {
  87. showModal: {
  88. alpha: {
  89. from: 0,
  90. to: 1,
  91. duration: 300
  92. }
  93. }
  94. }
  95. }
  96. ```
  97. </TabItem>
  98. <TabItem value="dismiss">
  99. The following example demonstrates how to dismiss a modal with a fade-out animation.
  100. ```js
  101. options: {
  102. animations: {
  103. dismissModal: {
  104. alpha: {
  105. from: 1,
  106. to: 0,
  107. duration: 300
  108. }
  109. }
  110. }
  111. }
  112. ```
  113. </TabItem>
  114. </Tabs>
  115. ## Shared element transitions
  116. 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.
  117. :::caution
  118. 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.
  119. :::
  120. ### Transition breakdown
  121. Let's take a more in-depth look at the following example, which you can find in the playground app:
  122. > [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)
  123. <p align="center">
  124. <img alt="Shared Element Transition" src={useBaseUrl('img/sharedElement.gif')} />
  125. </p>
  126. Four elements are animated in this example. Let's list the elements involved in the transition and note which properties are being animated.
  127. * **image** - the item image is animated to the next screen.
  128. * image scale (resizeMode)
  129. * position on screen
  130. * **image background** - each item has a "shadow" view which transitions to the next screen and turns into a colorful header.
  131. * scale
  132. * position on screen
  133. * color
  134. * **title** - the title of the item is animated to the next screen.
  135. * font size
  136. * font color
  137. * position on screen
  138. * **Description** - the description of the item in the destination screen.
  139. * fade-in
  140. * translation y
  141. ### Implementing shared element transitions
  142. #### Step 1 - set a nativeID prop to elements in the source screen
  143. In order for RNN to be able to detect the corresponding native views of the elements,
  144. each element must include a unique `nativeID` prop.
  145. ```jsx
  146. <Image
  147. source={item.image}
  148. nativeID={`image${item.id}`}
  149. style={styles.image}
  150. resizeMode={'contain'} />
  151. ```
  152. #### Step 2 - set a nativeID prop to elements in the destination screen
  153. ```jsx
  154. <Image
  155. source={this.props.image}
  156. nativeID={`image${this.props.id}Dest`}
  157. style={styles.image} />
  158. ```
  159. #### Step 3 - Declare the shared element animation when pushing the screen
  160. ```jsx
  161. Navigation.push(this.props.componentId, {
  162. component: {
  163. name: Screens.CocktailDetailsScreen,
  164. passProps: { ...item },
  165. options: {
  166. animations: {
  167. push: {
  168. sharedElementTransitions: [
  169. {
  170. fromId: `image${item.id}`,
  171. toId: `image${item.id}Dest`
  172. }
  173. ]
  174. }
  175. }
  176. }
  177. }
  178. });
  179. ```
  180. ## Element Transitions
  181. Element transitions also allow you to animate elements during shared element transitions.
  182. ### Recreating
  183. #### Step 1 - Set a nativeID prop to the element you'd like to animate
  184. An element can either be in the source or destination screen.
  185. ```jsx
  186. <Text
  187. nativeID='description'
  188. style={styles.description}>
  189. {this.props.description}
  190. </Text>
  191. ```
  192. #### Step 2 - Declare the element animation when pushing the screen
  193. ```jsx
  194. Navigation.push(this.props.componentId, {
  195. component: {
  196. name: Screens.CocktailDetailsScreen,
  197. passProps: { ...item },
  198. options: {
  199. animations: {
  200. push: {
  201. elementTransitions: [
  202. {
  203. id: 'description',
  204. alpha: {
  205. 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
  206. duration: SHORT_DURATION
  207. },
  208. translationY: {
  209. from: 16, // Animate translationY from 16dp to 0dp
  210. duration: SHORT_DURATION
  211. }
  212. }
  213. ]
  214. }
  215. }
  216. }
  217. }
  218. });
  219. ```
  220. ## Peek and Pop (iOS 11.4+)
  221. react-native-navigation supports [Peek and pop](
  222. https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/Adopting3DTouchOniPhone/#//apple_ref/doc/uid/TP40016543-CH1-SW3) feature in iOS 11.4 and newer.
  223. 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:
  224. ```jsx
  225. const handlePress ({ reactTag }) => {
  226. Navigation.push(this.props.componentId, {
  227. component {
  228. name: 'previewed.screen',
  229. options: {
  230. preview: {
  231. reactTag,
  232. height: 300,
  233. width: 300,
  234. commit: true,
  235. actions: [{
  236. title: "Displayed Name",
  237. id: "actionId",
  238. style: 'default', /* or 'selected', 'destructive'*/
  239. actions: [/*define a submenu of actions with the same options*/]
  240. }]
  241. },
  242. },
  243. },
  244. });
  245. };
  246. const Button = (
  247. <Navigation.TouchablePreview
  248. touchableComponent={TouchableHighlight}
  249. onPress={handlePress}
  250. onPressIn={handlePress}
  251. >
  252. <Text>My button</Text>
  253. </Navigation.TouchablePreview>
  254. );
  255. ```
  256. 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.