SwiftUI 按钮 if 语句

时间:2021-06-04 15:12:14

标签: if-statement button swiftui

我有以下问题:

我构建了一个有 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()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用 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 属性发生更改,您的视图都会重新呈现,因此您不一定必须使用类似 {{1 }} 触发更新。