首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-native reanimated不响应onFocus和onBlur

react-native reanimated不响应onFocus和onBlur
EN

Stack Overflow用户
提问于 2019-12-13 22:58:58
回答 1查看 508关注 0票数 0

我想要做的是

当文本输入被聚焦时,我需要对View的背景颜色进行动画处理。

我到现在为止所做的

我构建了一个具有两个函数onFocusonBlur的组件--它只是一个来自react- event -reanimated的react-native-reanimated函数。

代码语言:javascript
复制
 const onFocus = event<NativeSyntheticEvent<TextInputFocusEventData>>([
    {
      nativeEvent: ({ target }) => set(colorAnimation, 1)
    }
  ]);

  const onBlur = event<NativeSyntheticEvent<TextInputFocusEventData>>([
    {
      nativeEvent: ({ target }) => set(colorAnimation, 0)
    }
  ]);

我将这个函数传递给一个动画TextInput,我在控制台上没有得到任何错误,它显示了我设置的自定义背景色。但不会改变焦点或模糊。

代码

我正在使用typescript

代码语言:javascript
复制
import React from 'react';
import { NativeSyntheticEvent, TextInputFocusEventData } from 'react-native';
import Reanimated from 'react-native-reanimated';
import { bInterpolateColor } from 'react-native-redash';
import styled from 'styled-components/native';

const { Clock, Value, event, set, debug, block, divide } = Reanimated;

const StyledTextInput = styled.TextInput`
  height: 40px;
  padding: ${props => props.theme.spacing.default * 2}px
    ${props => props.theme.spacing.default * 3}px;
`;

const AnimatedTextInput: typeof StyledTextInput = Reanimated.createAnimatedComponent(
  StyledTextInput
);

const Container = styled(Reanimated.View)`
  width: 100%;
  border: 1px solid ${props => props.theme.colors.border};
  background: #e3e7eb;
  border-radius: ${props => props.theme.shapping.borderRadius * 2};
`;

const TextInput = () => {
  const clock = new Clock();
  const colorAnimation = new Value(0);

  const onFocus = event<NativeSyntheticEvent<TextInputFocusEventData>>([
    {
      nativeEvent: ({ target }) => set(colorAnimation, 1)
    }
  ]);

  const onBlur = event<NativeSyntheticEvent<TextInputFocusEventData>>([
    {
      nativeEvent: ({ target }) => set(colorAnimation, 0)
    }
  ]);

  return (
    <Container
      style={{
        backgroundColor: bInterpolateColor(
          colorAnimation,
          { r: 255, g: 0, b: 0 },
          { r: 0, g: 0, b: 255 }
        )
      }}
    >
      <AnimatedTextInput onFocus={onFocus} onBlur={onBlur} />
    </Container>
  );
};

export default TextInput;

结果

EN

回答 1

Stack Overflow用户

发布于 2020-03-09 09:26:20

据我所知,你没有正确使用binterpolatecolor。

从文档中:

代码语言:javascript
复制
const bInterpolateColor: (value: Animated.Adaptable<number>, color1: string | number, color2: string | number, colorSpace?: "hsv" | "rgb") => Animated.Node<number>;

你没有提供色彩空间(但这是可选的,所以我不知道它有多相关),而且颜色1和颜色2只能是字符串或数字,而且你似乎已经给出了一个对象。

所以这是第一个问题,我也不确定容器组件是否会通过setstate()更新而不进行某种状态更改,但我可能错了,因为我无论如何都不是一个活跃的专家。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59325034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档