如何在使用插值时初始设置不透明度

问题描述 投票:4回答:1

我有一个问题,在使用插值滚动值时,我无法将图像的初始不透明度设置为0。

因此,当我使用logoOpacity值Animated.View是完全可见的,但是例如,如果我使用__opacity它确实应用有效值,图像部分可见。

使用logoOpacity变量在我开始滚动时正确工作 - 这是关于初始值的全部 - 我无法设置隐藏视图。

如何在滚动时进行插值并启动Animated.View从0到100的可见性?

请参阅以下代码:

import React, { Component } from 'react';
import {
  Animated,
  Platform,
} from 'react-native';
import {connectStyle} from "@shoutem/theme/index";

import { View } from '@shoutem/ui/components/View'
import { Text } from '@shoutem/ui/components/Text'
import { Image } from '@shoutem/ui/components/Image'


import { Fonts, Colors, Layout } from '../../constants';

const HEADER_MAX_HEIGHT = 260; //
const HEADER_MIN_HEIGHT = 160; // Layout.NAVIGATION_HEADER_HEIGHT; // Platform.OS === 'ios' ? 60 : 73;
const HEADER_SCROLL_DISTANCE = HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT;

/**
 * https://medium.com/appandflow/react-native-scrollview-animated-header-10a18cb9469e
 *
 */
class OpportunityBlock extends Component {
  constructor(props) {
    super(props);

    this.state = {
      scrollY: new Animated.Value(
          // iOS has negative initial scroll value because content inset...
          Platform.OS === 'ios' ? -HEADER_MAX_HEIGHT : 0,
      )
    };
  }

  render() {

    const { style } = this.props

    // Because of content inset the scroll value will be negative on iOS so bring
    // it back to 0.
    const scrollY = Animated.add(
        this.state.scrollY,
        Platform.OS === 'ios' ? HEADER_MAX_HEIGHT : 0,
    );

    // Small logo animations
    const logoOpacity = scrollY.interpolate({
      inputRange: [0, HEADER_SCROLL_DISTANCE / 2, HEADER_SCROLL_DISTANCE],
      outputRange: [0, 0, 1],
      extrapolate: 'clamp',
    });

    const __opacity = new Animated.Value(0.3);

    return (
        <View styleName={"vertical"}>

          {/* MAIN CONTENT SECTION **/}
          <Animated.ScrollView
              style={{ flex: 1 }}
              scrollEventThrottle={1}
              onScroll={Animated.event(
                  [{
                    nativeEvent: {
                      contentOffset: { y: this.state.scrollY }
                    }
                  }],
                  { useNativeDriver: true },
              )}
          >

            <View style={style.scrollViewContent}>
              <Text>XX</Text>
            </View>
          </Animated.ScrollView>

          <Animated.View
              style={[
                style.logoContainer,
                {
                  opacity: logoOpacity,
                },
              ]}
          >
            <Image
                styleName={'small'}
                source={{ uri: item.images[0].url }}
            />
          </Animated.View>
        </View>
    );
  }
}

const style = {
  content: {
    flex: 1,
  },
  logoContainer: {
    position: 'absolute',
    top: 0,
    left:0,
    opacity:0,
    // right: -100, // initial position
    marginTop:30,
    paddingLeft:10,
  },

  scrollViewContent: {
    // iOS uses content inset, which acts like padding.
    paddingTop: Platform.OS !== 'ios' ? HEADER_MAX_HEIGHT : 0 // paddingTop: HEADER_MAX_HEIGHT // Platform.OS !== 'ios' ? HEADER_MAX_HEIGHT : 0,
  }
}

// connect the component to the theme
export default connectStyle('mbm.common.OpportunityBlock', style)(OpportunityBlock);

测试用例:似乎问题与scrollY动画值有关,因为那种情况不能正常工作,图像完全可见,即使是难度的scrollY也是0.也许有一些内在的滚动值?

const logoOpacityDoesNotWork = scrollY.interpolate({
  inputRange: [0, 0, 250],
  outputRange: [0.1, 0.1, 1],
  extrapolate: 'clamp',
});


const logoOpacityWorks = (new Animated.Value(120)).interpolate({
      inputRange: [0, 0, 250],
      outputRange: [0.1, 0.1, 1],
      extrapolate: 'clamp',
    });
react-native react-animated
1个回答
4
投票

尝试使用0.01作为初始值。有一个错误会将0变为null,这将导致忽略不透明度值。

© www.soinside.com 2019 - 2024. All rights reserved.