if-statement - SwiftUI 按钮 if 语句
问题描述
我有以下问题:
我构建了一个有 6 个按钮的应用程序,每个按钮都分配了一个 CL 区域。我有一个 GPS 轨迹文件,它为应用程序提供位置并模拟步行。
现在,当到达分配给它们的区域时,按钮应该变成黄色。到目前为止它有效,但问题是我不知道在哪里询问是否到达按钮区域。我尝试了 .onAppear () {if ...},但这只触发了一次,我需要一些东西来一直监视 If 语句。
有没有什么?
提前致谢
这是我的代码:
import MapKit
import SwiftUI
import CoreLocation
var region1 = CLCircularRegion(center:CLLocationCoordinate2D(latitude: 52.505088, longitude: 13.333574), radius: 20, identifier: "region1")
var region2 = CLCircularRegion(center:CLLocationCoordinate2D(latitude: 52.504829, longitude: 13.336798), radius: 20, identifier: "region2")
var region3 = CLCircularRegion(center:CLLocationCoordinate2D(latitude: 52.504733, longitude: 13.341834), radius: 20, identifier: "region3")
var region4 = CLCircularRegion(center:CLLocationCoordinate2D(latitude: 52.503312, longitude: 13.347718), radius: 20, identifier: "region4")
var region5 = CLCircularRegion(center:CLLocationCoordinate2D(latitude: 52.505699, longitude: 13.352198), radius: 20, identifier: "region5")
var region6 = CLCircularRegion(center:CLLocationCoordinate2D(latitude: 52.513517, longitude: 13.350253), radius: 20, identifier: "region6")
class LocationEnviroment: NSObject, CLLocationManagerDelegate, ObservableObject{
var locationManager = CLLocationManager()
@Published var currentPosition = ""
var Waypoints = [region1, region2, region3, region4, region5, region6];
@Published var RegionIndex: Int = 6
func initPositionService(){
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
func isWayPoint(mylocation : CLLocation) -> Int{
for i in self.Waypoints{
if(i == mylocation){
if(i.identifier == "region1"){
return 0
}else if(i.identifier == "region2"){
return 1
}else if(i.identifier == "region3"){
return 2
}else if(i.identifier == "region4"){
return 3
}else if(i.identifier == "region5"){
return 4
}else if(i.identifier == "region6"){
return 5
}
}
}
return 6 // <- nothing found
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let myLocation = locations.last
let lat = myLocation?.coordinate.latitude
let lon = myLocation?.coordinate.longitude
self.currentPosition = "lat: \(lat ?? 0) lon: \(lon ?? 0)"
self.RegionIndex = isWayPoint(mylocation: myLocation!)
}
}
struct ContentView: View {
@StateObject var locationEnviroment = LocationEnviroment()
var Todo = ["Friseur / Haare schneiden", "Dönerbude / zu Mittag essen", "Bank / Geld abheben", "Supermarkt / einkaufen", "Blumeneladen /\n Blumen kaufen", " Lisa's Wohnung /\n Lisa Blumen überreichen" ]
@State private var disabled = Array(repeating: true, count: 6)
@State private var red = 255.0
@State private var blue = 0.0
@State private var green = 0.0
var body: some View {
VStack(alignment: .center, spacing: 10){
Text(locationEnviroment.currentPosition)
.padding()
ForEach(0..<6) { row in
Button(action : {
if(disabled[row] == false){
red = 0.0
green = 255.0
blue = 0.0
}
}) {
Text(Todo[row])
.frame(width : 200, height: 40, alignment: .center)
.clipShape(Rectangle())
.padding()
.border(Color.black, width: 1)
.font(.system(size: 15))
.foregroundColor(.black)
}
.background(Color(red: red/255, green: green/255, blue: blue/255))
.lineLimit(nil)
.cornerRadius(8.0)
.disabled(disabled[row])
.onAppear(){
if(locationEnviroment.RegionIndex == row){
disabled[row] = false
red = 255.0
green = 255.0
blue = 0.0
}
}
}
}
.onAppear(){
locationEnviroment.initPositionService()
}
}
}
解决方案
您可以使用onChange
来监控 的状态RegionIndex
。它可能看起来像这样(替换你的onAppear
):
.onChange(of: locationEnviroment.RegionIndex) { regionIndex in
if regionIndex == row {
disabled[row] = false
red = 255.0
green = 255.0
blue = 0.0
}
}
一些旁注:
- 一般在 Swift 中,变量名和属性名以小写字母开头
- 您可能想研究使用计算属性来更改/确定颜色。我开始用这些写一个解决方案,但后来意识到我并不完全理解颜色系统的完整逻辑是什么,尤其是按下和禁用按钮之间的交互。但是,一般来说,您的视图将在任何时候重新呈现
@Published
您更改的属性@StateObject
,因此您不一定必须使用类似onChange
触发更新的东西。
推荐阅读
- php - 如何检查邮件是否已经注册(PHP)
- python - Selenium - 滚动屏幕截图
- javascript - React:Material-ui 网格没有响应 - 我该如何解决这个问题?
- arrays - C中两个排序数组中的最大和路径
- javascript - 无法在移动输入上修复图像方向
- reactjs - 不要 lint 元组中的键
- python - numpy 图像浮点数组,为 int 0..255 值
- swiftui - 在 SwiftUI 中隐藏 Picker 在 watchOS 上的焦点边框
- python - discord.py 机器人脚本对 list.pop() 函数没有反应。如何解决这个问题?
- javascript - Python web 抓取请求和漂亮的汤被需要 javascript 阻止