How do I work with background in React Native? - reactjs

In my app, I want to create some sort of parallax effect, just like this, where the content overlap the background : video
This is what I made in Figma: figma prototype
And this is what I currently have : current prototype
How do I allow the content to go over the picture and how am I supposed to only have the bottom of the picture for the background ? (I don't want to cut it since it could mess on other devices)
Here is my code :
import React from 'react'
import {ImageBackground,View, ScrollView, Text, StyleSheet, Dimensions} from 'react-native'
import {generalStyles} from '#gym-app/styles/general'
export default function MuscleScreen() {
const Image = require('../assets/muscles/abs.jpg')
return (
<ScrollView>
<ImageBackground source={Image} style={styles.background}>
<View style={[generalStyles.container, styles.overlap]}>
<Text style={generalStyles.title}>Abdominaux</Text>
</View>
</ImageBackground>
</ScrollView>
)
}
const styles = StyleSheet.create({
background: {
width: Dimensions.get('window').width,
height: (Dimensions.get('window').width / 16 * 9),
resizeMode: 'cover'
},
overlap: {
position: 'absolute',
top: (Dimensions.get('window').width / 16 * 9),
width: Dimensions.get('window').width
}
})
Thank you for your help !

Put your ImageBackground behind the ScrollView, e.g.
<View>
<ImageBackground />
<ScrollView />
<View>
Give ScrollView absolute position taking entire screen
As a first item in ScrollView, add blank space (empty View) with height of a
window you want for the picture
Capture current scroll position of
ScrollView with Animated.event
Interpolate that value so that it scrolls up slightly slower than ScrollView and apply it as transform translateY of your ImageBackground

Related

The Text inside <Text> tag does not show up on screen in react-native

I'm new to react-native and was following a tutorial. For some reason, the text in the <Text> tag inside <View> does not show up on the screen when I set flex:1 for the container. But when I comment it out I can see the text.
Here is the code.
import React from 'react';
import { View, Text, StyleSheet, Dimensions} from 'react-native';
export default function SignInScreen(){
return(
<View style = {styles.container}>
<View style ={{marginLeft:20, marginTop:10}}>
<Text style = {styles.title}>Sign-In</Text>
</View>
<View style = {{alignItems:"center",marginTop:10}}>
<Text style = {styles.text1}>Please Enter email address</Text>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex:1
},
title: {
color:"#ff8c52",
fontSize :20,
fontWeight:"bold"
},
text1: {
color: "#ff8c52",
fontSize:16
}
})
Can someone explain why this is so?
Flexbox fills the area that its parent gives it, so make sure that the parent component also uses flex or has a volume to it.

BottomTabNavigator coming on top instead of bottom in React Native expo mobile app

I am developing a mobile react native expo app. I am using BottomTabNavigator (NavigationContainer). As the name suggests it should appear at the bottom but it is incorrectly appearing on top.
I already have another image (logo.png) on the top of the screen and the navigationbar (or NavigationContainer) is also coming on top and overlapping above the image. Please help me resolve this issue. See my code below:
In the below code MyTabs is the Navigator created from createBottomTabNavigator(). This is incorrectly appearing on top of the screen.
import React from 'react';
import { Image, StyleSheet, Text, View, SafeAreaView, StatusBar, Platform } from 'react-native';
import logo from './assets/logo.png';
import { NavigationContainer } from '#react-navigation/native';
import MyTabs from './navigator/AppNavigator';
export default function App() {
return (
<SafeAreaView style={{ paddingTop: Platform.OS === 'android' ? StatusBar.currentHeight: 0 }} >
<View>
<View style={styles.container}>
<Image source={logo} style={{ width: 100, height: 100 }} />
<Text style={{color: '#888', fontSize: 18, alignItems: 'center'}}>
To share a photo from your phone with a friend or anyone, just press the button below!
</Text>
</View>
<View >
<NavigationContainer >
<MyTabs /> // This is incorrectly coming on top of screen.
</NavigationContainer>
</View>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
// justifyContent: 'center',
},
});
The NavigationContainer should be the outermost component in App. This then wraps the Tab.Navigator component (in your case MyTabs), where you create tabs linked to each of your components. Inside your components, you are able to utilize SafeAreaView to then display the image at the top of the screen. Any type of Navigation scheme has to be made the top most component in the hierarchy in react native, wrapping the rest of your components. I've altered your code below:
import React from 'react';
import { Image,  StyleSheet, Text, View, SafeAreaView, StatusBar, Platform } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
export default function App() {
  const Tab = createBottomTabNavigator()
  return (
    <NavigationContainer >
      <Tab.Navigator>  
        <Tab.Screen name="Home" component={myComponent} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}
const myComponent = () => {
  return (
    <SafeAreaView style={{ paddingTop: Platform.OS === 'android' ? StatusBar.currentHeight: 0 }} >
      <View>
        <View style={styles.container}>
          <Image source={require('#expo/snack-static/react-native-logo.png')} style={{ width: 100, height: 100 }} />
          <Text style={{color: '#888', fontSize: 18, alignItems: 'center'}}>To share a photo from your phone with a friend or anyone, just press the button below!</Text>
        </View>
      </View>
    </SafeAreaView>
  )
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
     alignItems: 'center',
    // justifyContent: 'center',
  },
});

Is there a way to wrap background color around text in react-native?

Currently, this is what I am trying to avoid. As you can see the background color runs until the end of the width. It would be nice for the background color to surround the text.
I looked a tons of examples and I don't really see where they wrap background color around the text itself
<Text style={{backgroundColor:'blue'}}> TTeexxtt </Text>
I tried with flexWrap and it doesn't work just like so
<Text style={{backgroundColor:'blue', flexWrap:'wrap'}}> TTeexxtt </Text>
As always, thank you
Views in react native default to an alignItems prop of stretch. What does this mean though?
All children of your view will stretch to fill the width of their parent, with considerations for margin.
If you set the alignItems property of your parent view to something other than stretch, such as center, flex-start, or flex-end you will see the background color only extend around your actual text.
You can also use alignSelf on your Text views to adjust individual items.
Here is an example snack
https://snack.expo.io/#ajthyng/vengeful-celery
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
const BGText = props => {
const { background } = props;
return <Text style={{backgroundColor: background, ...props.style}}>{props.children}</Text>;
}
export default class App extends React.Component {
render() {
return (
<View style={{flex: 1, backgroundColor: 'white', alignItems: 'stretch', marginTop: 23}}>
<BGText background='pink' style={{marginRight: 16}}>I am some text</BGText>
<BGText background='orange' style={{alignSelf: 'flex-start', marginLeft: 16}} >My BG Color is short</BGText>
</View>
);
}
}

Simple React Native Image Game is Lagging.How do i optimize?

I am trying to implement a simple img tapping game with react native for learning purposes.I am using this librarys https://github.com/bberak/react-native-game-engine game loop component as a game loop.
What I am trying to do is render some imgs with random locations on screen with small radius then increase it up to a value, after decrease it and finally remove the img from screen (or when tapped) - just like in this game: http://mouseaccuracy.com/
import React, { Component } from "react";
import { AppRegistry, StyleSheet, Dimensions, View,ToastAndroid,TouchableOpacity,Image,Text } from "react-native";
const { width: WIDTH, height: HEIGHT } = Dimensions.get("window");
import { GameLoop } from "react-native-game-engine";
import { Fonts } from '../utils/Fonts';
import FastImage from 'react-native-fast-image'
import * as Progress from 'react-native-progress';
import SwordImage from "../assets/tapfight/sword.png";
import ShieldImage from "../assets/tapfight/shield.png";
this.renderSwordOrCircle(circle)
)
})
)
}
}
renderHealth = () =>{
return(
<View>
<View style={{ alignItems:'center' ,position: "absolute", top: 0, left: 0}}>
<Text style={{fontFamily:Fonts.MainFont,color:'white',fontSize:25}}>{this.playerHealth}</Text>
<Progress.Bar color='red' progress={this.playerHealth/100} width={100} height={18} />
</View>
<View style={{ alignItems:'center',position: "absolute", top: 0, right: 0}}>
<Text style={{fontFamily:Fonts.MainFont,color:'white',fontSize:25}}>{this.enemyHealth}</Text>
<Progress.Bar color='red' progress={this.enemyHealth/100} width={100} height={18} />
<FastImage resizeMode="contain" style={{width:100,height:100}} source={require('../assets/tapfight/guard_2_head.png')}></FastImage>
</View>
</View>
)
}
render() {
if(this.state.fight==true){
return (
<View style={{flex:1,backgroundColor:'transparent'}} >
<GameLoop style={{flex:1}} onUpdate={this.updateCircle}>
{this.renderHealth()}
{this.renderCircles()}
</GameLoop>
</View>
);
}else{
return(
null
)
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
}
});
What i am doing is i am generating images every 600 ms on screen and player tries to tap them.The problem is when there is more than 2 images on screen fps drops significantly.Tested in android actual device.I am updating state once at the end of updateCircle function.
Game loop is updateCircle component
Your problem is that you're trying to animate a component that is not made for being animated.
I dont know well the FastImage component, but I understand that it purpose is to pre-load images, not animate.
You shoud use Animated.Image for the image, and Animated.timing for managing the animations of Animated.Image

How to implement a horizontal scrollview like an appstore

How to implement a scrollview like an appstore?
I'm going to scroll to the next content, when the scroll is done halfway
You can implement it simple with FlatList, using horizontal and pagingEnabled boolean props and deviceWidth;
import React from 'react';
import { StyleSheet, Dimensions, ScrollView, View, Text } from 'react-native';
const deviceWidth = Dimensions.get('window').width;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
page: {
width: deviceWidth
},
});
const Slider = () => (
<View style={styles.container}>
<ScrollView
horizontal
showsHorizontalScrollIndicator={false}
pagingEnabled>
<View style={styles.page}>
<Text> This is View 1 </Text>
</View>
<View style={styles.page}>
<Text> This is View 2 </Text>
</View>
</ScrollView>
</View>
);
The feature or functionality you are looking for is a simple horizontal slider.
Use could use this npm library to get what you are looking for: https://www.npmjs.com/package/react-native-snap-carousel
I recommend using Flatlist which is React-Native's own component.
If you want to swipe the data Horizontally the use the prop horizontal={true} in the Flatlist.
Something like this is what you want?
Source code: https://snack.expo.io/#gusgard/react-native-swiper-flatlist-2
Library: https://www.npmjs.com/package/react-native-swiper-flatlist

Resources