React-Native动态标题栏(仿微信)

项目需要写了一个动态的标题栏。原来是没有一点想法的。后来想到了。
方法不难。就是在ScrollView向上滑动时,会掉用onScroll方法。在onScrollView方法中根据滑动的偏移量,而后修改state。从而实现动态效果。
先看看效果。


test.gif

核心代码

onScroll(evt){
        let y = evt.nativeEvent.contentOffset.y;
        // 隐藏返回按键和照相机
        if(y >= width06 && y<=width08){
            opcityNumber = 1-(y-width06)/(width02);
            this.setState({
                opcityNumber:opcityNumber,
            })
        }
        // 原本以为是文字的,不过看着微信的效果太平滑了。所以我就弄成照片了。
        // 开始显示朋友圈图片。
        if(y>=width08&&y<=width09 && this.state.friendShow == false){
            this.setState({
                friendShow:true,
            })
        }
        // 朋友圈图片动态效果
        if(y>=width08&&y<=width09){
            let friendHeight = (1-(y-width08)/(width01))*10+40;
            let friendShowOpactiy = (y-width08)/(width01);
            this.setState({
                friendHeight:friendHeight,
                friendShowOpactiy:friendShowOpactiy,
            })
        }

        //隐藏朋友圈照片
        if(y<=width08 && this.state.friendShow == true){
            this.setState({
                friendShow:false,
            })
        }
    }

全部代码

import React, {
    Component,
} from 'react';
import {
    StyleSheet,
    View,
    ScrollView,
    StatusBar,
    Image,
    Dimensions,
} from 'react-native';

let {width,height} = Dimensions.get('window');
const width01 = width*0.1;
const width02 = width*0.2;
const width06 = width*0.6;
const width08 = width*0.8;
const width09 = width*0.9;
export default class TestView1 extends Component {
    // 构造
    constructor(props) {
        super(props);
       this.flag = true;
       this.state={
           opcityNumber:1,
           friendShow:false,
           friendShowOpactiy:1,
           friendHeight:50,
       }
    }

    static navigationOptions = {
        header:null,
    };

    render() {
        return (
            <View >
                <StatusBar
                    translucent = {true}
                    backgroundColor = {'#00000000'}
                    barStyle = {this.state.friendShow?('dark-content'):('light-content')}
                />
                {/* 返回按键和照相机图片 */}
                <View 
                    style={{
                        flexDirection:'row',
                        justifyContent:'space-between',
                        position:'absolute',
                        width:width,
                        height:50,
                        top:20,
                        zIndex:2,
                        alignItems:'center',
                        paddingLeft:20,
                        paddingRight:20,
                        opacity:this.state.opcityNumber,
                    }}

                >
                    <Image style={{width:32,height:32}} source={require('../../resources/image/back_black_white.png')}>
                    </Image>
                    <Image style={{width:32,height:32}}  source={require('../../resources/image/camera.png')}>

                    </Image>
                </View>
                {/* 朋友圈照片。 */}
                {this.state.friendShow?(
                    <View 
                        style={{
                            position:'absolute',
                            height:20+this.state.friendHeight,
                            paddingTop:20,
                            width:width,
                            backgroundColor:'rgba(2555,255,255,'+this.state.friendShowOpactiy+')',
                            zIndex:3,
                    }}>
                        <View style={{
                            height:this.state.friendHeight,
                            justifyContent:'center',
                            left:this.state.friendHeight*0.5,
                        }}>
                            
                            <Image 
                                style = {{width:100*(this.state.friendHeight/50),height:this.state.friendHeight*0.8}}
                                source = {require('../../resources/image/friend.png')}
                            >

                            </Image>
                        </View>
                    </View>
                ):(<View></View>)}
                <ScrollView
                    showsVerticalScrollIndicator = {false}
                    onScroll = {(evt)=>this.onScroll(evt)}
                    scrollEventThrottle = {16}
                >
                    <Image source={require('../../resources/image/cooker/bg.png')} 
                        style={{width:width,height:width}}>

                    </Image>
                    <View style={{height:height}}>
                    </View>
                </ScrollView>
            </View>
        );
    }
    onScroll(evt){
        let y = evt.nativeEvent.contentOffset.y;
        // 隐藏返回按键和照相机
        if(y >= width06 && y<=![back_black_white.png](https://upload-images.jianshu.io/upload_images/12082766-4ebd62e10241a735.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
width08){
            opcityNumber = 1-(y-width06)/(width02);
            this.setState({
                opcityNumber:opcityNumber,
            })
        }
        // 原本以为是文字的,不过看着微信的效果太平滑了。所以我就弄成照片了。
        // 开始显示朋友圈图片。
        if(y>=width08&&y<=width09 && this.state.friendShow == false){
            this.setState({
                friendShow:true,
            })
        }
        // 朋友圈图片动态效果
        if(y>=width08&&y<=width09){
            let friendHeight = (1-(y-width08)/(width01))*10+40;
            let friendShowOpactiy = (y-width08)/(width01);
            this.setState({
                friendHeight:friendHeight,
                friendShowOpactiy:friendShowOpactiy,
            })
        }

        //隐藏朋友圈照片
        if(y<=width08 && this.state.friendShow == true){
            this.setState({
                friendShow:false,
            })
        }
    }
}

另外本代码中使用的照片(因为是白色的,所以看不到)


back_black_white.png
camera.png
friend.png

如有其它想法,欢迎交流。
另外我没有找到可以设置背景色的方式,如果有请告知。谢谢。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,074评论 3 119
  • 尽管我还没看完《挪威的森林》,但书页俨然快见底了,也就不大影响我如今蓬勃的思绪。毕竟,一个人看到这些和自己如此类...
    般若观阅读 3,573评论 0 3
  • 01 一个有品位的女人,总是着装得体的。不在乎她穿的衣服什么牌子、什么面料。搭配恰到好处,这是有品位的女人最基本的...
    许6569阅读 10,234评论 0 2
  • 忽然很想写封情书给你 告诉你我有多爱你 自从见到你的那一刻起 便注定今生再也无法分离 忽然很想写封情书给你 你那撅...
    一滴露水阅读 3,621评论 13 11
  • 明天都在纠结,要不要来简书写点字呢?写还是不写呢?写什么主题呢?唉……于是今天小天使打败了小恶魔还是爬上来写点东西...
    叶轻筠阅读 3,093评论 2 3

友情链接更多精彩内容