React Native Animated Multiple Animations on one element - css

Is there a way to have multiple animations on a single Animated View, changing the same css property?
this.expandAnimation = new Animated.Value(50);
this.shrinkAnimation = new Animated.Value(200);
Animated.sequence([
Animated.timing(this.expandAnimation, {
toValue: 200,
duration: 1000
}),
Animated.timing(this.shrinkAnimation, {
toValue: 50,
duration: 1000
})
]),
<Animated.View style={[styles.box, { width: this.ExpandAnimation }]}> // What value to bind to width?
<View>
</View>
</Animated.View>

Answered by Federkun. You use a single animated value.
this.expandAndShrinkAnimation = new Animated.Value(50);
Animated.sequence([
Animated.timing(this.expandAndShrinkAnimation, {
toValue: 200,
duration: 1000
}),
Animated.timing(this.expandAndShrinkAnimation, {
toValue: 50,
duration: 1000
})
]),
<Animated.View style={[styles.box, { width: this.expandAndShrinkAnimation }]}> // What value to bind to width?
<View>
</View>
</Animated.View>

If you run the Animation on Parallel see this Demo link

Related

How to add if conditions to theme breakpoints in styling

I want to have no breakpoint specific changes if my variant is horizontalCard. On the other hand, I want these media query breakpoints for other variants.
Suppose I have this variant argument sent like this. Ignore the closing tags
export const useStyles = makeStyles<
{ variant: string },
‘hasTooltip’ | ‘large’ | ‘medium’ | ‘small’ | ‘checked’ | ‘icon’ | ‘label’
>({
name: ‘CardFormControlLabel’,
})((...[theme, { variant }, classes]) => ({
root: {
`
And I want the labelRoot class to have conditional media query breakpoint
labelRoot: {
flexDirection: variant === ‘card’ ? ‘column-reverse’ : ‘row-reverse’,
height: ‘calc(100% - 2px)’,
marginLeft: 0,
width: ‘calc(100% - 2px)‘,
zIndex: 2,
borderRadius,
‘&.Mui-disabled’: {
backgroundColor: theme.palette.grey[50],
borderRadius,
},
[theme.breakpoints.down(‘md’)]: {
flexDirection: ‘row’,
},
[`.${classes.large} &`]: {
...theme.mixins.shadow2,
alignItems: ‘normal’,
backgroundColor: theme.palette.grey[50],
borderRadius,
},
},
Can I use something like this??? Seems wrong to me
[theme.breakpoints.down(‘md’) && variant != ‘horizontalCard]: {
flexDirection: ‘row’,
},

How to get useChain to run animations sequentially?

I'm trying to use react-spring to run animation X and then do animation Y. I'm using the useChain feature. It seems to run everything in parallel. What am I doing wrong? This is a super-simple scenario and I'm surprised it doesn't work AND my Googling has not turned up a lot of other people with the same problem and/or a solution. There are bugs on useChain in react-spring but they seem to cover more limited scenarios.
As an aside, I thought all React components needed to start with a capital letter so how does animated.div pass the compiler?
code sandbox simple example
This is my component. The box moves diagonally, not in the X axis and then in the Y axis.
export const Shaker = () => {
const xRef = useSpringRef();
const { x } = useSpring({
from: { x: 0 },
to: { x: 1 },
config: config.molasses,
ref: xRef
});
const yRef = useSpringRef();
const { y } = useSpring({
from: { y: 0 },
to: { y: 1 },
config: config.molasses,
ref: yRef
});
useChain([xRef, yRef]);
return (
<animated.div
style={{
width: "10rem",
height: "10rem",
backgroundColor: "green",
marginLeft: x.to((i) => i * 600 - 300),
marginTop: y.to((i) => i * 600 - 300)
}}
></animated.div>
);
};
They say this is a bug in react-spring but don't provide the number of the duplicate.
https://github.com/pmndrs/react-spring/issues/1605

React spring loop won't run with delay

I'm using these styles with react spring, and after it runs one time, it won't run anymore, if I remove the delay it works fine, why is that?
const styles = useSpring({
loop: true,
to: [
{ opacity: 0, color: '#53317e' },
{ opacity: 1, color: 'white' },
],
from: { opacity: 1, color: 'white' },
delay: 1000,
})
lot of time gone but I had same issue.
To fix it, instead of using
const styles = useSpring({
loop: true,
delay: 2000,
from: {...},
to: [{...}, {...}, ...]
});
I used syntax with passing function instead of object
const [styles, api] = useSpring(() => ({ ... }) )
And then delay prop stops disabling my loop

cropping snapshot in react native maps to just routes

I would like to know if anyone knows how to take snapshot of just routes in react native maps, something what ride sharing companies have when a user views previous trips. I am able to take a snapshot using the code below, though I noticed the height, width, region properties do not.
takeLocationSnapshot() {
const { location: { longitude, latitude, } } = this.state;
const snapshot = this.mapView.takeSnapshot({
width: 300, // optional, when omitted the view-width is used
height: 300, // optional, when omitted the view-height is used
region: {
latitude,
longitude,
LONGITUDEDELTA,
LATITUDEDELTA,
},
// iOS only, optional region to render
format: 'jpg', // image formats: 'png', 'jpg' (default: 'png')
quality: 0.7, // image quality: 0..1 (only relevant for jpg, default: 1)
result: 'file' // result types: 'file', 'base64' (default: 'file')
});
snapshot.then((uri) => {
this.props.locationSnapshotChange(uri);
});
}
<View style={styles.imageContentWrap}>
<Image
source={imageSource}
style={styles.imageStyle}
/>
</View>
imageContentWrap: {
flexWrap: 'wrap',
backgroundColor: 'green',
flex: 0.58,
},
imageStyle: {
flexWrap: 'wrap',
width: '100%',
height: '100%',
resizeMode: 'cover',
overflow: 'hidden',
},
Just using Wrapper with fixed height and overflow hidden, and add position absolute, top property to styles of your image

Changing createMaterialTopTabNavigator default styling

I have createMaterialTopTabNavigator in my app with three tabs. These three tabs themselves belong to different createStackNavigators. I have passed drawer icon as my header right to createMaterialTopTabNavigator.
I want to edit the background color of createMaterialTopTabNavigator tabs but it is getting override with my HeaderRight icon styling.
const Daily = createStackNavigator(
{
Daily: {
screen: DailyStack,
},
Another:{
screen: Another,
}
},
{
headerMode:'none'
},
);
const Monthly = createStackNavigator({
Monthly: {
screen: MonthlyStack,
},
},
{
headerMode:'none'
});
const Range = createStackNavigator({
Range: {
screen: RangeStack,
}
},
{
headerMode:'none'
});
const DashboardTabNavigator = createMaterialTopTabNavigator(
{
Daily,
Monthly,
Range
},
{
navigationOptions: ({ navigation }) => {
return {
// tabBarOptions:{
// indicatorStyle: {
// backgroundColor: "#2E86C1",
// },
// // tabStyle:{
// // backgroundColor: '#F7F9F9'
// // },
// labelStyle :{
// color: '#2E86C1'
// },
// activeTintColor:'blue',
// inactiveTintColor: {
// color: 'green'
// },
// style: {
// backgroundColor: 'white',
// elevation: 0, // remove shadow on Android
// shadowOpacity: 0, // remove shadow on iOS,
// borderWidth:1,
// borderColor:'#ccc'
// }
// },
headerRight: (
<Icon style={{ paddingRight:20 }} onPress={() => navigation.openDrawer()} name="menu" color='#000' size={30} />
)
};
}
}
)
If I am passing the styling options inside navigationOptions then the styling does not works; only HeaderRight shows, and if I pass the styling options outside the navigationOptions, the styling works but then it hides the HeaderRight Icon from right
you must entirely study this link.
another important subject is that navigationOptions related to every screen in stack. such as this:
const App = createMaterialTopTabNavigator({
TabScreen: {
screen: TabScreen,
navigationOptions: {
headerStyle: {
backgroundColor: '#633689',
},
headerTintColor: '#FFFFFF',
title: 'TabExample',
},
},
});
so if you want to set style for top tab bar, you must use defaultNavigationOptions property such as this:
const DashboardTabNavigator = createMaterialTopTabNavigator(
{
Daily,
Monthly,
Range
},
{
defaultNavigationOptions: ({ navigation }) => {
return {
tabBarOptions:{
style: {
backgroundColor: 'white',
elevation: 0, // remove shadow on Android
shadowOpacity: 0, // remove shadow on iOS,
borderWidth:1,
borderColor:'#ccc'
}
},
};
}
}
)
Sharing common navigationOptions across screens
It is common to want to configure the header in a similar way across many screens. For example, your company brand color might be red and so you want the header background color to be red and tint color to be white. Conveniently, these are the colors we're using in our running example, and you'll notice that when you navigate to the DetailsScreen the colors go back to the defaults. Wouldn't it be awful if we had to copy the navigationOptions header style properties from HomeScreen to DetailsScreen, and for every single screen component we use in our app? Thankfully, we do not. We can instead move the configuration up to the stack navigator under the property defaultNavigationOptions.

Resources