From 64f128cb55ae82ec936c768604ab1ed1eff7ceb7 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Mon, 18 Dec 2023 14:36:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=AD=8C=E5=8D=95=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E6=92=AD=E6=94=BE=E6=95=B0=E9=87=8F=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/common/Text.tsx | 39 ++++++++++++++++++++++++--- src/screens/SonglistDetail/Header.tsx | 32 ++++++++++++++++------ src/utils/hooks/useAnimateNumber.ts | 33 ++++++++++++++++++++--- 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/src/components/common/Text.tsx b/src/components/common/Text.tsx index 532bd77..c9ac45d 100644 --- a/src/components/common/Text.tsx +++ b/src/components/common/Text.tsx @@ -3,7 +3,7 @@ import { Text, type TextProps as _TextProps, StyleSheet, Animated, type ColorVal import { useTheme } from '@/store/theme/hook' import { setSpText } from '@/utils/pixelRatio' import { useAnimateColor } from '@/utils/hooks/useAnimateColor' -import { useAnimateNumber } from '@/utils/hooks/useAnimateNumber' +import { DEFAULT_DURATION, useAnimateNumber } from '@/utils/hooks/useAnimateNumber' // import { AppColors } from '@/theme' export interface TextProps extends _TextProps { @@ -17,17 +17,50 @@ export interface TextProps extends _TextProps { color?: ColorValue } +// const warpText =

(Component: ComponentType) => { +// return ({ style, size = 15, color, children, ...props }: P) => { +// const theme = useTheme() +// return ( +// {children} +// ) +// } +// } + export default ({ style, size = 15, color, children, ...props }: TextProps) => { const theme = useTheme() return ( {children} ) } +export interface AnimatedTextProps extends _AnimatedTextProps { + /** + * 字体大小 + */ + size?: number + /** + * 字体颜色 + */ + color?: ColorValue +} +export const AnimatedText = ({ style, size = 15, color, children, ...props }: AnimatedTextProps) => { + const theme = useTheme() + + return ( + {children} + ) +} + type _AnimatedTextProps = ComponentProps<(typeof Animated)['Text']> export interface AnimatedColorTextProps extends _AnimatedTextProps { @@ -48,7 +81,7 @@ export const AnimatedColorText = ({ style, size = 15, opacity: _opacity, color: const theme = useTheme() const [color] = useAnimateColor(_color ?? theme['c-font']) - const [opacity] = useAnimateNumber(_opacity ?? 1) + const [opacity] = useAnimateNumber(_opacity ?? 1, DEFAULT_DURATION, false) return ( { + const [animFade] = useAnimateOnecNumber(0, 1, 250, false) + const [animTranslateY] = useAnimateOnecNumber(10, 0, 250, false) + return ( + {count} + ) +}, (prevProps, nextProps) => { + return true +}) + const Pic = ({ componentId, playCount, imgUrl }: { componentId: string playCount: string @@ -28,12 +45,10 @@ const Pic = ({ componentId, playCount, imgUrl }: { return ( - - { - playCount && animated - ? {playCount} - : null - } + + { + playCount && animated ? : null + } ) } @@ -95,6 +110,7 @@ const styles = createStyle({ // backgroundColor: '#eee', flexGrow: 0, flexShrink: 0, + overflow: 'hidden', // width: 70, // height: 70, // ...Platform.select({ diff --git a/src/utils/hooks/useAnimateNumber.ts b/src/utils/hooks/useAnimateNumber.ts index 7529334..f07ea9b 100644 --- a/src/utils/hooks/useAnimateNumber.ts +++ b/src/utils/hooks/useAnimateNumber.ts @@ -2,9 +2,9 @@ import { useEffect, useMemo, useRef, useState } from 'react' import { Animated } from 'react-native' -const ANIMATION_DURATION = 800 +export const DEFAULT_DURATION = 800 -export const useAnimateNumber = (val: number) => { +export const useAnimateNumber = (val: number, duration = DEFAULT_DURATION, useNativeDriver = true) => { const anim = useMemo(() => new Animated.Value(0), [val]) const [finished, setFinished] = useState(true) const currentNumber = useRef(val) @@ -19,8 +19,8 @@ export const useAnimateNumber = (val: number) => { setFinished(false) Animated.timing(anim, { toValue: 1, - duration: ANIMATION_DURATION, - useNativeDriver: false, + duration, + useNativeDriver, }).start((finished) => { if (!finished) return // currentNumber.current = nextNumber @@ -33,3 +33,28 @@ export const useAnimateNumber = (val: number) => { return [animNumber, finished] as const } + +export const useAnimateOnecNumber = (val: number, toVal: number, duration = DEFAULT_DURATION, useNativeDriver = true) => { + const anim = useMemo(() => new Animated.Value(0), []) + const [finished, setFinished] = useState(true) + + const animNumber = anim.interpolate({ + inputRange: [0, 1], + outputRange: [val, toVal], + }) + + useEffect(() => { + setFinished(false) + Animated.timing(anim, { + toValue: 1, + duration, + useNativeDriver, + }).start((finished) => { + if (!finished) return + // currentNumber.current = nextNumber + setFinished(true) + }) + }, []) + + return [animNumber, finished] as const +}