React Native中的透明覆盖

时间:2015-06-04 08:27:59

标签: javascript reactjs react-native

我正在尝试在应用中滑动透明叠加层,这里非常类似(全部/过滤器):

transparent slider

到目前为止,我发现了react-native-slider和react-native-overlay。我将滑块修改为从上到下工作,但它总是沿着ListView向下移动。如果使用react-native-overlay,叠加层是静态的,我无法移动它。

我在原始反应原生教程in this gist中添加了一些演示代码。单击按钮时,内容应该粘住,菜单应叠加。透明度现在并不重要,但会很棒。

最聪明的解决方案是什么?

6 个答案:

答案 0 :(得分:57)

ListView不向下移动的关键是将叠加层的位置设置为absolute。通过这样做,您可以手动设置视图的位置和宽度/高度,它不再遵循flexbox布局。看看下面的简短示例。叠加层的高度固定为360,但您可以轻松地为其设置动画或使其动态化。

'use strict';

var React = require('react-native');
var Dimensions = require('Dimensions');

// We can use this to make the overlay fill the entire width
var { width, height } = Dimensions.get('window');

var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

var SampleApp = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to the React Native Playground!
        </Text>
        <View style={[styles.overlay, { height: 360}]} />
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  // Flex to fill, position absolute, 
  // Fixed left/top, and the width set to the window width
  overlay: {
    flex: 1,
    position: 'absolute',
    left: 0,
    top: 0,
    opacity: 0.5,
    backgroundColor: 'black',
    width: width
  }  
});

AppRegistry.registerComponent('SampleApp', () => SampleApp);

module.exports = SampleApp;

答案 1 :(得分:12)

background image with overlay

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

export default class App extends Component {
  render() {
    return (
      <Image source={{uri: 'http://i.imgur.com/IGlBYaC.jpg'}} style={s.backgroundImage}>
        <View style={s.overlay}/>
      </Image>
    );
  }
}

const s = StyleSheet.create({
  backgroundImage: {
      flex: 1,
      width: null,
      height: null,
  },
  overlay: {
    position: 'absolute',
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    backgroundColor: 'red',
    opacity: 0.3
  }
});

现场演示:https://sketch.expo.io/S15Lt3vjg

来源回购:https://github.com/Dorian/sketch-reactive-native-apps

答案 2 :(得分:2)

您可以使用此示例来创建叠加层。可以将状态更改为可见,将不可见状态更改为叠加。

import React, { Component } from "react";
import { View, Text, StyleSheet } from "react-native";

class Popup extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isPopupTrue: true
    };
  }

  render() {
    return (
      <View style={styles.container}>
      { this.state.isPopupTrue &&
        (<View style={styles.overlay}>
          <View style={styles.popup}>
            <Text style={styles.text}> Overlay </Text>
          </View>
        </View>)
      }
      </View>
    );
  }
}

export const styles = StyleSheet.create({
  container: {
    flex:1,
    backgroundColor: "#c0d6e4"
  },
  overlay: {
    position: "absolute",
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "gray",
    opacity: 0.9,
  },
  text: {
    width: "20%",
    fontSize: 15,
    color: "black",
    fontWeight: "bold"
  },
});

答案 3 :(得分:1)

最聪明的方式?

对我来说最聪明的方法是使用来自react-native的Modals来构建高度可自定义的响应式体验,您可以轻松设置模态的运动方向,设置透明度,切换可见性等等,我个人从未使用现有的npm模块来实现侧抽屉或导航栏, 我是用Modals做的 如果您希望我能提供一个示例代码片段,该代码片段使用Modals

实现导航抽屉

答案 4 :(得分:1)

也许更好地使用ImageBackground - 组件。

import {View, ImageBackground, Text} from 'react-native';

const styles = StyleSheet.create({

});
...
<ImageBackground
  style={styles.image}
  source={{uri: props.picture_url}}
>
   <View style={styles.textbox}>
      <Text style={styles.title} >CHILD OF IMAGE_BACKGROUND</Text >
    </View>
 </ImageBackground >
...

答案 5 :(得分:0)

我也遇到了同样的问题,

import {View,StyleSheet} from "react-native";
//where you want it to render
 <View style={styles.overlaycontainer}>
      <Text style={{color:"#fff"}}>Locating directions please wait ...</Text>        
 </View> 
// at the bottom of your in styles
const styles = StyleSheet.create({
  overlaycontainer:{
    ...StyleSheet.absoluteFillObject,
    backgroundColor: '#000',
    opacity:0.8,
    justifyContent:"center",
    alignItems:"center" 
  }
});