javascript - 如何通过 MapView 区域更改更改 TouchableOpacity 颜色?
问题描述
考虑这段代码:
import React, { useState, useEffect } from 'react';
import { StyleSheet, Image, View, ScrollView, Text, FlatList, TouchableOpacity } from 'react-native';
import MapView, { Marker } from 'react-native-maps';
import {requestPermissionsAsync, getCurrentPositionAsync} from 'expo-location';
import { TouchableHighlight, TextInput } from 'react-native-gesture-handler';
import { MaterialIcons } from '@expo/vector-icons';
import SearchBar from '../../components/searchInput';
function Main(){
const[currentRegion, setCurrentRegion ] = useState(null);
var color = 'red';
function changeColor(){
color = 'blue';
};
useEffect(()=>{
async function loadInitialPosition() {
const { granted } = await requestPermissionsAsync();
if(granted){
const { coords } = await getCurrentPositionAsync({
enableHighAccuracy: true,
});
const { latitude, longitude } = coords;
userCoords = coords;
setCurrentRegion({
latitude,
longitude,
latitudeDelta: 0.01,
longitudeDelta: 0.01,
})
}
}
return (
<>
<MapView
ref = {(mapView) => { map = mapView; }}
onRegionChange={()=>{
changeColor()
}}
initialRegion={ currentRegion }
style={styles.map}
customMapStyle={Mapstyle}
showsMyLocationButton={true}
showsUserLocation={true}
>
{satiros.localizarSatiros()}
</MapView>
<View style={styles.searchContainer}>
<SearchBar/>
</View>
<View style={styles.extraMap}>
<TouchableHighlight onPress={() => {}} style={styles.touchable}>
<MaterialIcons name="search" size={25} color="#9BAED4" />
</TouchableHighlight>
<TouchableHighlight onPress={()=>{
mapboxModule.centralizarLocalizacao(map,userCoords.latitude,userCoords.longitude,60)
}} style={styles.touchable2}>
<MaterialIcons name="my-location" size={25} color="#9BAED4" />
</TouchableHighlight>
</View>
<ScrollView pagingEnabled horizontal style={styles.scrollCards} showsHorizontalScrollIndicator={false}>
{
images.map((item, index) => (
<TouchableOpacity
activeOpacity={0.6}
style={[styles.cardSlide,{backgroundColor:color}]}
>
<Image key={index} source={{ uri: item}}
style={styles.imagesCard}/>
<Text style={styles.cardTitle}>{MapSlide.card.nome}</Text>
<Text style={styles.cardSubTitle}>{MapSlide.card.desc}</Text>
</TouchableOpacity >
))
}
</ScrollView>
</>
);
}
const styles = StyleSheet.create(styleJaja)
export default Main;
我想在用户移动地图时更改 TouchableOpacity 的 backgroundColor 值。为了检测他是否在移动,我使用调用函数“changeColor”的“onRegionChange”,而不是将“颜色”变量更改为蓝色。到目前为止,一切都很好。'color' 变量值确实发生了变化。
但是这种变化不会影响 backgroundColor 的颜色,只会影响变量。任何想法?
解决方案
也许您可以尝试创建一个新useState
的来控制和设置颜色,例如:
const[color, setColor ] = useState('red');
和
changeColor(){ setColor ('blue'); };
推荐阅读
- javafx - Javafx 组合框
- typescript - 如何让 TypeScript 知道我已经通过了 `modelValue`?
- python - 使用 Stomp 订阅 ActiveMQ Artemis 时遇到问题。队列已存在
- docker - 在 dockerfile 中安装 haskell 的 cabal 或 ghcup 不起作用
- python - if/else 语句仅在找到匹配项时运行
- amazon-web-services - S3 存储桶策略 IAM 角色显示为 API 密钥
- typescript - 想要连续运行 Google Cloud SQL 迁移
- html - 如何使用 puppeteer 点击标签
- arrays - 如何在反应中将对象值数组更改为对象值数组对象
- angular - 如何让图像在 Angular 的 jspdf 自动表中工作