react native 自定義導(dǎo)航欄隨滾動漸變

效果圖1 未滾動頁面

image.png

效果圖2 滾動頁面

image.png

使用方式

import React, { Component } from 'react'
import NavPage from './NavPage'
import { View, ScrollView, Image, Dimensions, Platform } from 'react-native'

let { width } = Dimensions.get('window')
let navHeight = (Platform.OS === 'ios' ? 20 : 0) + 45

export default class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      opacity: 0
    }
  }

  onScroll = (event) => {
    let offsetY = event.nativeEvent.contentOffset.y
    let opacity = offsetY / navHeight
    // if(opacity > 5 || opacity < -5) { // 這里可以優(yōu)化減少render, 1和0 滑快了會有些影響, 這里你可以看著給值, 當(dāng)然也可以不優(yōu)化
    //   return
    // }
    this.setState({
      opacity
    }) 
  }

  render() {
    return (
      <View style={{ alignItems: 'center' }}>
        <NavPage title={'詳情頁'} opacity={this.state.opacity} />
        <ScrollView
          showsVerticalScrollIndicator={false}
          ref='scroll'
          onScroll={this.onScroll}
          scrollEventThrottle={10}>
          <Image style={{ width: width, height: 300 }} source={{ uri: 'https://dimg07.c-ctrip.com/images/100e0t000000ihd4r494C_C_500_280.jpg' }} />
          <Image style={{ width: width, height: 300, marginTop: 20 }} source={{ uri: 'https://dimg04.c-ctrip.com/images/300e0q000000g5o8b658A_C_500_280.jpg' }} />
          <Image style={{ width: width, height: 300, marginTop: 20 }} source={{ uri: 'https://dimg04.c-ctrip.com/images/100l0s000000hg344225B_C_500_280.jpg' }} />
          <Image style={{ width: width, height: 300, marginTop: 20 }} source={{ uri: 'https://youimg1.c-ctrip.com/target/100f0d0000006xaav4A5E_C_220_110.jpg' }} />
        </ScrollView>
      </View>
    )
  }
}

自定義導(dǎo)航 NavPage.js

import React, { Component } from 'react';
import { View, Text, Image, StyleSheet, TouchableOpacity, Platform, Dimensions } from 'react-native';


/**
 * 自定義導(dǎo)航欄
 */
let height = (Platform.OS === 'ios' ? 20 : 0) + 45
export default class NavPage extends Component {

  render() {
    let { opacity, children, title } = this.props
    return (
      <View style={[styles.container, { backgroundColor: `rgba(16,94,174, ${opacity})` }]}>
        <TouchableOpacity style={styles.item} onPress={() => { alert('返回') }}>
          <Image style={styles.icon} source={require('./arrow.png')} />
        </TouchableOpacity>
        {

          <View style={{ alignItems: 'center', flex: 1 }}>
            {
              opacity < 0.4 ? null :
                (children || <Text style={{ color: '#FFF', fontSize: 17 }}>{title}</Text>)
            }
          </View>
        }
        <TouchableOpacity style={styles.item} onPress={() => { alert('更多') }}>
          <Image style={[styles.icon, { width: 25, height: 5 }]} source={require('./more.png')} />
        </TouchableOpacity>

      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    width: Dimensions.get('window').width,
    height: height,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingTop: Platform.OS === 'ios' ? 20 : 0,
    paddingHorizontal: 10,
    position: 'absolute',
    zIndex: 10
  },
  icon: {
    width: 21,
    height: 21,
  },
  item: {
    height: 30,
    width: 30,
    justifyContent: 'center',
    alignItems: 'center'
  }
})

NavPage.defaultProps = {
  title: 'title',
  opacity: 0
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容