SwiftUI地图相机运动

问题描述

我正在尝试使地图相机在全球范围内连续进行动画处理。目前,我正在尝试在地图加载时更改onAppear中的coordinateRegions纬度。我正在使用计时器每秒更新一次纬度。到目前为止,我收到错误消息“在视图更新过程中修改状态,这将导致未定义的行为;但是,已更新的纬度正在控制台中打印,但是地图的ordinateRegion尚未更新。”我想知道是否有更好的方法通过swiftUI对全球地图进行连续动画处理。

感谢任何帮助, 谢谢

import SwiftUI
import MapKit

struct MapView: View {

var timer = Timer()
@State var startlocation = CLLocationCoordinate2DMake(41.851,-87.6238)
@State var endLatitude = 41.91 //North Avenue Beach

@State private var region = MKCoordinateRegion(
    center: CLLocationCoordinate2D(latitude: 41.851,longitude: -87.6238),span: MKCoordinateSpan(latitudeDelta: 40,longitudeDelta: 40)
)

func moveRegion() {
    var currentLatitude = region.center.latitude
           let increment = 0.00005
           Timer.scheduledTimer(withTimeInterval: (1.0/30.0),repeats: true) { (timer) in
               currentLatitude += increment
               if currentLatitude >= self.endLatitude {
                   timer.invalidate()
               }
            region.center.latitude = currentLatitude
            print(currentLatitude)
           }
}

var body: some View {
    Map(coordinateRegion: $region,annotationItems: sites) { location in
        MapAnnotation(
            coordinate: location.coordinate,anchorPoint: CGPoint(x: 0.5,y: 0.5)
        ) {
            VStack {
                Circle()
                    .overlay(
                    Circle()
                        .stroke(location.color.opacity(0.8),lineWidth: 3))
                    .foregroundColor(location.color.opacity(0.5))
                    .frame(width: location.radius,height: location.radius)
            }
            
            }
    }.onAppear {
        moveRegion()
    }

}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...