问题
So I just copy these codes of MapView with Custom Animated Markers and Region Focus when is Scrolled in this url. My question is, how can I use the interpolate or the Animated Marker the same as when I press the onPress in my Card View?
So the feature will be two now, one is an Animated Marker when is Scrolled and two when is Pressed.
I already added how to animate to its Region, now my goal is how can I use Animated Markers when I press the onPress of an item the same as the code when it is scrolled?
Here are the codes for Animated Marker when is Scrolled:
UNSAFE_componentWillMount(){
this.index = 0;
this.animation = new Animated.Value(0);
}
componentDidMount() {
this.askMultiPermissionOnAndroid();
this.animation.addListener(({ value }) => {
let index = Math.floor(value / CARD_WIDTH + 0.3); // animate 30% away from landing on the next item
if (index >= this.props.eventsState.length) {
index = this.props.eventsState.length - 1;
}
if (index <= 0) {
index = 0;
}
clearTimeout(this.regionTimeout);
this.regionTimeout = setTimeout(() => {
if (this.index !== index) {
this.index = index;
const { latitude, longitude } = this.props.eventsState[index];
this.map.animateToRegion(
{
latitude,
longitude,
latitudeDelta: this.state.focusedLocation.latitudeDelta,
longitudeDelta: this.state.focusedLocation.longitudeDelta,
},
350
);
}
}, 10);
});
}
render () {
//For the Scroll View Card
const interpolations = this.props.eventsState.map((marker, index) => {
const inputRange = [
(index - 1) * CARD_WIDTH,
index * CARD_WIDTH,
((index + 1) * CARD_WIDTH),
];
const scale = this.animation.interpolate({
inputRange,
outputRange: [1, 2.5, 1],
extrapolate: "clamp",
});
const opacity = this.animation.interpolate({
inputRange,
outputRange: [0.35, 1, 0.35],
extrapolate: "clamp",
});
return { scale, opacity };
});
return (
<MapView
style={styles.map}
initialRegion={{
latitude: 14.6317303,
longitude: 121.0324869,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421
}}
onPress={this.pickLocationHandler}
ref={ref => this.map = ref}
provider={PROVIDER_GOOGLE}
>
{this.props.eventsState.map((marker, index) => {
const scaleStyle = {
transform: [
{
scale: interpolations[index].scale,
},
],
};
const opacityStyle = {
opacity: interpolations[index].opacity,
};
return (
<Marker
key={index}
coordinate={{latitude: marker.latitude, longitude: marker.longitude}}
onPress={() => {
navigate('EventDetail', {
eventId: marker.id
});
}}
>
<Animated.View style={[styles.markerWrap, opacityStyle]}>
<Animated.View style={[styles.ring, scaleStyle]} />
<View style={styles.marker} />
</Animated.View>
</Marker>
);
})}
</MapView>
<Animated.FlatList
horizontal
scrollEventThrottle={1}
pagingEnabled={true}
showsHorizontalScrollIndicator={false}
snapToInterval={SNAP_INTERVAL}
onScroll={Animated.event(
[
{
nativeEvent: {
contentOffset: {
x: this.animation,
},
},
},
],
{ useNativeDriver: true }
)}
style={styles.scrollView}
contentContainerStyle={styles.endPadding}
data={this.props.eventsState}
keyExtractor={item => item.id}
renderItem={(itemData) => (
<View style={styles.event}>
<View style={styles.touchable}>
<TouchableCmp
onPress={() => {
//ANIMATED MARKER HERE WHEN onPress
this.map.animateToRegion({
latitude: itemData.item.latitude,
longitude: itemData.item.longitude,
latitudeDelta: this.state.focusedLocation.latitudeDelta,
longitudeDelta: this.state.focusedLocation.longitudeDelta
}, 350)
}}
useForeground
>
<View>
<ImageBackground source={{uri: itemData.item.imageUrl}} style={styles.image}>
<View style={styles.textContainer}>
<Text numberOfLines={1} style={styles.title}>{itemData.item.title}</Text>
<Text numberOfLines={2} style={styles.description}>{itemData.item.description}</Text>
</View>
</ImageBackground>
</View>
</TouchableCmp>
</View>
</View>
)}
/>
);
}
来源:https://stackoverflow.com/questions/59046836/using-same-animation-in-marker-when-i-press-onpress