Skip to content

Commit 399b04c

Browse files
committed
docs: add rotate-scale-fade-in-out layout
1 parent 1455d68 commit 399b04c

File tree

17 files changed

+196
-115
lines changed

17 files changed

+196
-115
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ English | [简体中文](./README.zh-CN.md)
6060
<a href="./example/src/scale-fade-in-out/index.tsx">
6161
<img src="assets/scale-fade-in-out.gif" width="300"/>
6262
</a>
63+
<a href="./example/src/rotate-scale-fade-in-out/index.tsx">
64+
<img src="assets/rotate-scale-fade-in-out.gif" width="300"/>
65+
</a>
6366
</p>
6467

6568
## Table of contents

README.zh-CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
<a href="./example/src/scale-fade-in-out/index.tsx">
6262
<img src="assets/scale-fade-in-out.gif" width="300"/>
6363
</a>
64+
<a href="./example/src/rotate-scale-fade-in-out/index.tsx">
65+
<img src="assets/rotate-scale-fade-in-out.gif" width="300"/>
66+
</a>
6467
</p>
6568

6669
## 目录
1.6 MB
Loading

docs/custom-animation.md

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ After some effort, we finally implemented custom animation in v2, now we just ne
88
type TAnimationStyle = (value: number) => Animated.AnimatedStyleProp<ViewStyle>;
99
```
1010

11-
1211
This function will be called in each item and accepts a parameter `value` indicating the position of the current item relative to `window`. The following picture shows the relationship between `value` and position
1312

14-
<img src="././../assets/custom-animation-sketch.png" width="300"/>
13+
<img src="././../assets/custom-animation-sketch.png" width="300"/>
1514

1615
After getting the `value`, we only need to describe how the item is displayed in the corresponding position, and the rest is handed over to `Animated` to execute.
1716

1817
### Tips
1918

20-
* Don't forget to set `zIndex`
19+
- Don't forget to set `zIndex`
2120

2221
---
2322

@@ -32,24 +31,21 @@ Here are a few examples.
3231
</a>
3332

3433
```ts
35-
const animationStyle: TAnimationStyle = React.useCallback(
36-
(value: number) => {
37-
'worklet';
38-
39-
const zIndex = interpolate(value, [-1, 0, 1], [10, 20, 30]);
40-
const translateX = interpolate(
41-
value,
42-
[-1, 0, 1],
43-
[-PAGE_WIDTH * 0.5, 0, PAGE_WIDTH]
44-
);
34+
const animationStyle: TAnimationStyle = React.useCallback((value: number) => {
35+
'worklet';
36+
37+
const zIndex = interpolate(value, [-1, 0, 1], [10, 20, 30]);
38+
const translateX = interpolate(
39+
value,
40+
[-1, 0, 1],
41+
[-PAGE_WIDTH * 0.5, 0, PAGE_WIDTH]
42+
);
4543

46-
return {
47-
transform: [{ translateX }],
48-
zIndex,
49-
};
50-
},
51-
[]
52-
);
44+
return {
45+
transform: [{ translateX }],
46+
zIndex,
47+
};
48+
}, []);
5349

5450
<Carousel
5551
style={{ width: screen.width, height: 240 }}
@@ -65,7 +61,7 @@ const animationStyle: TAnimationStyle = React.useCallback(
6561
/>
6662
);
6763
}}
68-
/>
64+
/>;
6965

7066
const CustomItem = ({ index, animationValue }) => {
7167
const maskStyle = useAnimatedStyle(() => {
@@ -98,7 +94,7 @@ const CustomItem = ({ index, animationValue }) => {
9894
/>
9995
</View>
10096
);
101-
}
97+
};
10298
```
10399

104100
In order to implement some animation effects outside `Carousel`, such as `MaskView`, we pass the animation value calculated inside each Item to the outside through `renderItem`.
@@ -110,22 +106,19 @@ In order to implement some animation effects outside `Carousel`, such as `MaskVi
110106
</a>
111107

112108
```ts
113-
const animationStyle: TAnimationStyle = React.useCallback(
114-
(value: number) => {
115-
'worklet';
109+
const animationStyle: TAnimationStyle = React.useCallback((value: number) => {
110+
'worklet';
116111

117-
const zIndex = interpolate(value, [-1, 0, 1], [10, 20, 30]);
118-
const scale = interpolate(value, [-1, 0, 1], [1.25, 1, 0.25]);
119-
const opacity = interpolate(value, [-0.75, 0, 1], [0, 1, 0]);
112+
const zIndex = interpolate(value, [-1, 0, 1], [10, 20, 30]);
113+
const scale = interpolate(value, [-1, 0, 1], [1.25, 1, 0.25]);
114+
const opacity = interpolate(value, [-0.75, 0, 1], [0, 1, 0]);
120115

121-
return {
122-
transform: [{ scale }],
123-
zIndex,
124-
opacity,
125-
};
126-
},
127-
[]
128-
);
116+
return {
117+
transform: [{ scale }],
118+
zIndex,
119+
opacity,
120+
};
121+
}, []);
129122

130123
<Carousel
131124
style={{
@@ -139,16 +132,7 @@ const animationStyle: TAnimationStyle = React.useCallback(
139132
data={[...new Array(6).keys()]}
140133
customAnimation={animationStyle}
141134
renderItem={({ index }) => {
142-
return (
143-
<SBItem
144-
key={index}
145-
index={index}
146-
style={{
147-
borderWidth: 5,
148-
borderColor: Colors.orange50,
149-
}}
150-
/>
151-
);
135+
return <SBItem key={index} index={index} />;
152136
}}
153-
/>
154-
```
137+
/>;
138+
```

docs/custom-animation.zh-CN.md

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# 自定义动画
22

3-
经过一些努力,我们终于在v2版本中实现了自定义动画,现在只需实现一个类型`TAnimationStyle`的回调函数,然后将它传给`Carousel``customAnimation`属性即可。
3+
经过一些努力,我们终于在 v2 版本中实现了自定义动画,现在只需实现一个类型`TAnimationStyle`的回调函数,然后将它传给`Carousel``customAnimation`属性即可。
44

55
## 准备
66

77
```
88
type TAnimationStyle = (value: number) => Animated.AnimatedStyleProp<ViewStyle>;
99
```
1010

11-
这个函数会在每个item里去调用,并接受一个参数`value`表示当前item相对`window`所在的位置,下图展示了`value`和位置的关系
11+
这个函数会在每个 item 里去调用,并接受一个参数`value`表示当前 item 相对`window`所在的位置,下图展示了`value`和位置的关系
1212

13-
<img src="././../assets/custom-animation-sketch.png" width="300"/>
13+
<img src="././../assets/custom-animation-sketch.png" width="300"/>
1414

15-
当拿到`value`后,我们只需要描述item在对应位置上如何展示,剩下的就交给`Animated`去执行吧。
15+
当拿到`value`后,我们只需要描述 item 在对应位置上如何展示,剩下的就交给`Animated`去执行吧。
1616

1717
### 小贴士
1818

19-
* 别忘了设置`zIndex`
19+
- 别忘了设置`zIndex`
2020

2121
---
2222

@@ -31,24 +31,21 @@ type TAnimationStyle = (value: number) => Animated.AnimatedStyleProp<ViewStyle>;
3131
</a>
3232

3333
```ts
34-
const animationStyle: TAnimationStyle = React.useCallback(
35-
(value: number) => {
36-
'worklet';
34+
const animationStyle: TAnimationStyle = React.useCallback((value: number) => {
35+
'worklet';
36+
37+
const zIndex = interpolate(value, [-1, 0, 1], [10, 20, 30]);
38+
const translateX = interpolate(
39+
value,
40+
[-1, 0, 1],
41+
[-PAGE_WIDTH * 0.5, 0, PAGE_WIDTH]
42+
);
3743

38-
const zIndex = interpolate(value, [-1, 0, 1], [10, 20, 30]);
39-
const translateX = interpolate(
40-
value,
41-
[-1, 0, 1],
42-
[-PAGE_WIDTH * 0.5, 0, PAGE_WIDTH]
43-
);
44-
45-
return {
46-
transform: [{ translateX }],
47-
zIndex,
48-
};
49-
},
50-
[]
51-
);
44+
return {
45+
transform: [{ translateX }],
46+
zIndex,
47+
};
48+
}, []);
5249

5350
<Carousel
5451
style={{ width: screen.width, height: 240 }}
@@ -64,7 +61,7 @@ const animationStyle: TAnimationStyle = React.useCallback(
6461
/>
6562
);
6663
}}
67-
/>
64+
/>;
6865

6966
const CustomItem = ({ index, animationValue }) => {
7067
const maskStyle = useAnimatedStyle(() => {
@@ -97,10 +94,10 @@ const CustomItem = ({ index, animationValue }) => {
9794
/>
9895
</View>
9996
);
100-
}
97+
};
10198
```
10299

103-
为了让`Carousel`外部实现一些动画效果,比如`MaskView`,我们将每个Item内部计算出来的动画值通过`renderItem`传递到了外部。
100+
为了让`Carousel`外部实现一些动画效果,比如`MaskView`,我们将每个 Item 内部计算出来的动画值通过`renderItem`传递到了外部。
104101

105102
### 缩放渐入渐出效果
106103

@@ -109,22 +106,19 @@ const CustomItem = ({ index, animationValue }) => {
109106
</a>
110107

111108
```ts
112-
const animationStyle: TAnimationStyle = React.useCallback(
113-
(value: number) => {
114-
'worklet';
109+
const animationStyle: TAnimationStyle = React.useCallback((value: number) => {
110+
'worklet';
115111

116-
const zIndex = interpolate(value, [-1, 0, 1], [10, 20, 30]);
117-
const scale = interpolate(value, [-1, 0, 1], [1.25, 1, 0.25]);
118-
const opacity = interpolate(value, [-0.75, 0, 1], [0, 1, 0]);
112+
const zIndex = interpolate(value, [-1, 0, 1], [10, 20, 30]);
113+
const scale = interpolate(value, [-1, 0, 1], [1.25, 1, 0.25]);
114+
const opacity = interpolate(value, [-0.75, 0, 1], [0, 1, 0]);
119115

120-
return {
121-
transform: [{ scale }],
122-
zIndex,
123-
opacity,
124-
};
125-
},
126-
[]
127-
);
116+
return {
117+
transform: [{ scale }],
118+
zIndex,
119+
opacity,
120+
};
121+
}, []);
128122

129123
<Carousel
130124
style={{
@@ -138,16 +132,7 @@ const animationStyle: TAnimationStyle = React.useCallback(
138132
data={[...new Array(6).keys()]}
139133
customAnimation={animationStyle}
140134
renderItem={({ index }) => {
141-
return (
142-
<SBItem
143-
key={index}
144-
index={index}
145-
style={{
146-
borderWidth: 5,
147-
borderColor: Colors.orange50,
148-
}}
149-
/>
150-
);
135+
return <SBItem key={index} index={index} />;
151136
}}
152-
/>
153-
```
137+
/>;
138+
```

example/src/advanced-parallax/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Carousel from 'react-native-reanimated-carousel';
99
import type { TAnimationStyle } from '../../../src/layouts/BaseLayout';
1010
import { SBItem } from '../components/SBItem';
1111
import SButton from '../components/SButton';
12+
import { ElementsText } from '../constants';
1213

1314
const window = Dimensions.get('window');
1415
const PAGE_WIDTH = window.width;
@@ -58,7 +59,7 @@ function Index() {
5859
setIsAutoPlay(!isAutoPlay);
5960
}}
6061
>
61-
AutoPlay:{`${isAutoPlay}`}
62+
{ElementsText.AUTOPLAY}:{`${isAutoPlay}`}
6263
</SButton>
6364
</View>
6465
);

example/src/constants/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const ElementsText = {
2+
AUTOPLAY: 'AutoPlay',
3+
};

example/src/home/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const LayoutsPage: Array<Record<'name', keyof RootStackParamList>> = [
2929
{
3030
name: 'ScaleFadeInOut',
3131
},
32+
{
33+
name: 'RotateScaleFadeInOut',
34+
},
3235
];
3336

3437
const OtherPage: Array<Record<'name', keyof RootStackParamList>> = [
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react';
2+
import SButton from '../components/SButton';
3+
4+
export function useToggleButton(opts: {
5+
defaultValue: boolean;
6+
buttonTitle: string;
7+
}) {
8+
const { buttonTitle, defaultValue = false } = opts;
9+
const [status, setStatus] = React.useState(defaultValue);
10+
11+
const button = React.useMemo(() => {
12+
return (
13+
<SButton onPress={() => setStatus(!status)}>
14+
{buttonTitle}:{`${status}`}
15+
</SButton>
16+
);
17+
}, [status, buttonTitle]);
18+
19+
return {
20+
status,
21+
button,
22+
};
23+
}

example/src/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import SnapCarouselLoopComponent from './snap-carousel-loop';
1717
import AdvancedParallaxComponent from './advanced-parallax';
1818
import PauseAdvancedParallaxComponent from './pause-advanced-parallax';
1919
import ScaleFadeInOutComponent from './scale-fade-in-out';
20+
import RotateScaleFadeInOutComponent from './rotate-scale-fade-in-out';
2021

2122
const Stack = createNativeStackNavigator<RootStackParamList>();
2223

@@ -29,6 +30,7 @@ export type RootStackParamList = {
2930
AdvancedParallax: undefined;
3031
PauseAdvancedParallax: undefined;
3132
ScaleFadeInOut: undefined;
33+
RotateScaleFadeInOut: undefined;
3234

3335
SnapCarouselComplex: undefined;
3436
SnapCarouselLoop: undefined;
@@ -77,6 +79,10 @@ function App() {
7779
name="ScaleFadeInOut"
7880
component={ScaleFadeInOutComponent}
7981
/>
82+
<Stack.Screen
83+
name="RotateScaleFadeInOut"
84+
component={RotateScaleFadeInOutComponent}
85+
/>
8086

8187
<Stack.Screen
8288
name="SnapCarouselComplex"

0 commit comments

Comments
 (0)