초기의 애니메이션은 자바스크립트 엔진이 기본으로 제공하는
requestAnimation Frame API를 이용한 자바스크립트 엔진 애니메이션을 사용했지만
requestAnimation Frame 함수를 반복 호출하면 다른 UI 컴포넌트의 동작이 정지하는 버그가 생겼음
useNativeDriver = true || false;
const animValue = new Animated.Value(0);
animVlaue.setValue(100);
export default function SomeComponent() {
const animValue = new Animated.Value(0) // 재 랜더링 시 마다 생성
const animValue2 = useRef(new Animated.Value(0)).current // 최초 랜더링 시 생성
}
RefObject<T>
타입 객체를 반환하는 버전 MutableRefObject<T>
타입 객체를 반환하는 버전 MutableRefObject 타입 객체를 반환하는 useRef 훅
function useRef<T>(initialValue: T): MutableRefOjbect<T>
MutableRefObject는 current 속성이 포함되어 있다는 것을 명심한다.
const animValue = useRef(new Animated.Value(0)).current;
type AnimatedComponent = Animated.createAnimatedComponent
export function createAnimatedComponent<T>(component:T): AnimatedComponent<T>
예시
Animated.View = Animated.createAnimatedComponent(View);
export const Animated.timing: (
value: Animated.Value | Animated.ValueXY,
config: Animated.TimingAnimationConfig
) => Animated.CompositeAnimation;
export interface CompositeAnimation {
start: (callback?: EndCAllback)=> void;
}
type EndResult = {finished: boolean};
type EndCallback = (result: EndResult) => void;
여기서 사용되는 EndCallback에서 종료 시점에 대한 callback 함수를 지정이 가능하다.
Animated.timing(animValue, {toValue: 1, duration: 1000, useNativeDriver: true}) .start(()=>console.log('animated end'));
export class Value {
addLisenter(callback: ValueListenrCallback): void;
removeListenr(id: string): void;
removeAllListeners(): void;
}
type ValueListenrCallback = (state: {value: number}) => void;
실제 보간 값을 얻어서 화면에 출력하는 예