gpt4 book ai didi

react-native - 使用插值时如何初始设置不透明度

转载 作者:行者123 更新时间:2023-12-01 00:14:02 25 4
gpt4 key购买 nike

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

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

使用 logoOpacity当我开始滚动时,变量确实正常工作 - 这完全是关于初始值 - 我无法设置隐藏 View 。

如何实现在滚动时插入并从 0 到 100 启动 Animated.View 可见性?

请看下面的代码:

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',
});

最佳答案

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

关于react-native - 使用插值时如何初始设置不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54972369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com