幫我將以上code修改我的代碼:import SwiftUI
import MapKit
extension CLLocationCoordinate2D: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(latitude)
hasher.combine(longitude)
}
public static func == (lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
}
}
struct TimelineApp: View {
@State private var selectedYear: Int = 756
@State private var mapRegion = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 0, longitude: 0),
span: MKCoordinateSpan(latitudeDelta: 90, longitudeDelta: 180)
)
@State private var mapAnnotations: [CustomMapAnnotation] = []
@State private var selectedEvent: Event?
let events: [Event] = [
// 示例事件(需要提供每个事件的坐标)
Event(country: "德國", title: "日耳曼部落時代", startYear: -500, endYear: 771, color: .gray, coordinate: CLLocationCoordinate2D(latitude: 51.1657, longitude: 10.4515)), // 德國座標
Event(country: "希臘", title: "雅典的全盛時期", startYear: -500, endYear: -322, color: .orange, coordinate: CLLocationCoordinate2D(latitude: 37.9838, longitude: 23.7275)), // 希臘座標
Event(country: "中國", title: "商朝", startYear: -1600, endYear: -1046, color: .red, coordinate: CLLocationCoordinate2D(latitude: 35.8617, longitude: 104.1954)), // 中國座標
Event(country: "中國", title: "唐朝建立", startYear: 618, endYear: 907, color: .red, coordinate: CLLocationCoordinate2D(latitude: 35.8617, longitude: 104.1954)), // 中國座標
Event(country: "英國", title: "盎格鲁-撒克逊时代", startYear: 410, endYear: 1066, color: .green, coordinate: CLLocationCoordinate2D(latitude: 51.509865, longitude: -0.118092)), // 英國座標
Event(country: "法國", title: "卡洛林帝国", startYear: 800, endYear: 888, color: .blue, coordinate: CLLocationCoordinate2D(latitude: 48.8566, longitude: 2.3522)), // 法國座標
Event(country: "俄羅斯", title: "基輔罗斯", startYear: 862, endYear: 1240, color: .purple, coordinate: CLLocationCoordinate2D(latitude: 55.7558, longitude: 37.6176)), // 俄羅斯座標
Event(country: "意大利", title: "洛伦佐大公统治", startYear: 1469, endYear: 1492, color: .orange, coordinate: CLLocationCoordinate2D(latitude: 41.9028, longitude: 12.4964)), // 意大利座標
Event(country: "西班牙", title: "西班牙王国成立", startYear: 1479, endYear: 1516, color: .red, coordinate: CLLocationCoordinate2D(latitude: 40.4168, longitude: -3.7038)), // 西班牙座標
Event(country: "土耳其", title: "奥斯曼帝国建立", startYear: 1299, endYear: 1922, color: .brown, coordinate: CLLocationCoordinate2D(latitude: 41.0082, longitude: 28.9784)), // 土耳其座標
Event(country: "埃及", title: "古埃及王國", startYear: -3100, endYear: -2686, color: .yellow, coordinate: CLLocationCoordinate2D(latitude: 30.0444, longitude: 31.2357)) // 埃及座標
]
func updateMapAnnotations() {
let filteredEvents = events.filter {
$0.startYear <= selectedYear && (selectedYear <= $0.endYear || $0.endYear == 0)
}
var annotations: [CustomMapAnnotation] = []
var usedLocations: Set<CLLocationCoordinate2D> = []
for event in filteredEvents {
var coordinate = event.coordinate
while usedLocations.contains(coordinate) {
// 稍微偏移坐标,直到找到一个未使用的位置
coordinate = CLLocationCoordinate2D(
latitude: coordinate.latitude + 0.1, // 调整偏移量
longitude: coordinate.longitude + 0.1 // 调整偏移量
)
}
annotations.append(CustomMapAnnotation(coordinate: coordinate, title: event.title))
usedLocations.insert(coordinate)
}
mapAnnotations = annotations
}
var body: some View {
NavigationView {
VStack {
// 顶部状态栏和年份
HStack {
Text("\(formattedYear(selectedYear))")
.background(Color.white.opacity(0.8))
.cornerRadius(10)
}
.padding()
// 世界地图
Map(coordinateRegion: $mapRegion, interactionModes: .all, showsUserLocation: false, userTrackingMode: nil, annotationItems: mapAnnotations) { annotation in
MapAnnotation(coordinate: annotation.coordinate) {
VStack {
Image(systemName: "pin.circle.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(.blue)
.onTapGesture {
// 在点击标注时设置选中的事件
if let event = events.first(where: { $0.title == annotation.title }) {
selectedEvent = event
}
}
Text(annotation.title ?? "")
.font(.caption)
}
}
}
.frame(height: 200)
.edgesIgnoringSafeArea(.all)
.onAppear {
updateMapAnnotations()
}
.onChange(of: selectedYear) { newValue, oldValue in
updateMapAnnotations()
}
// 时间轴
TimelineView(selectedYear: $selectedYear)
.frame(height: 100)
// 事件列表
ScrollView {
EventList(selectedYear: selectedYear, selectedEvent: $selectedEvent, events: events)
}
}
.navigationBarHidden(true)
// 添加 NavigationLink,根据 selectedEvent 的值跳转到详情视图
.sheet(item: $selectedEvent) { event in
EventDetailView(event: event)
}
}
}
func formattedYear(_ year: Int) -> String {
return year >= 0 ? "\(year) CE" : "\(abs(year)) BCE"
}
}
struct TimelineView: View {
@Binding var selectedYear: Int
let startYear = -3100
let endYear = 2300
let smallTickInterval = 1
let largeTickInterval = 10
let visibleYearRange = 10
@State private var viewportWidth: CGFloat = UIScreen.main.bounds.width - 40
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .leading) {
Rectangle()
.fill(Color.blue.opacity(0.2))
.frame(height: 2)
.offset(x: 1)
// 绘制刻度和标签
ForEach(visibleYears, id: \.self) { year in
VStack {
Rectangle()
.fill(Color.blue)
.frame(width: 1, height: year % largeTickInterval == 0 ? 10 : 5)
.position(x: positionForYear(year), y: 5)
if year % largeTickInterval == 0 {
Text(formattedYear(year))
.font(.caption)
.rotationEffect(.degrees(-60))
.position(x: positionForYear(year), y: 30)
}
}
}
// 选择器
Rectangle()
.fill(Color.red)
.frame(width: 2, height: 50)
.position(x: positionForYear(selectedYear), y: 25)
.gesture(
DragGesture()
.onChanged { value in
let newYear = yearForPosition(value.location.x)
selectedYear = min(max(startYear, newYear), 2024)
}
)
}
.onAppear {
viewportWidth = geometry.size.width
}
}
.frame(height: 100)
.padding(.horizontal)
}
var visibleYears: [Int] {
let start = max(startYear, selectedYear - visibleYearRange / 2)
let end = min(endYear, selectedYear + visibleYearRange / 2)
return Array(stride(from: start, through: end, by: smallTickInterval))
}
func formattedYear(_ year: Int) -> String {
return year >= 0 ? "\(year)" : "\(abs(year)) BCE"
}
private var yearWidth: CGFloat {
viewportWidth / CGFloat(endYear - startYear)
}
private func positionForYear(_ year: Int) -> CGFloat {
CGFloat(year - startYear) * yearWidth + 20 // Adjusted for starting position
}
private func yearForPosition(_ position: CGFloat) -> Int {
let adjustedPosition = position - 20
let year = Int(adjustedPosition / yearWidth) + startYear
return year
}
}
struct EventList: View {
let selectedYear: Int
@Binding var selectedEvent: Event?
let events: [Event]
var body: some View {
VStack(alignment: .leading) {
let filteredEvents = events.filter {
$0.startYear <= selectedYear && (selectedYear <= $0.endYear || $0.endYear == 0)
}
let groupedEvents = Dictionary(grouping: filteredEvents, by: { $0.country })
ForEach(groupedEvents.keys.sorted(), id: \.self) { country in
HStack(alignment: .top) {
Text(country)
.font(.headline)
.frame(width: 60, alignment: .leading)
Rectangle()
.fill(Color.gray)
.frame(width: 1)
LazyVStack(alignment: .leading, spacing: 20) {
ForEach(groupedEvents[country] ?? []) { event in
if event.startYear != 0 {
Button(action: {
// 选择事件
selectedEvent = event
}) {
EventBar(event: event, selectedYear: selectedYear, eventCount: groupedEvents[country]?.count ?? 1)
}
} else {
Rectangle()
.fill(event.color.opacity(0.5))
.frame(height: 20)
}
}
}
}
.padding(.bottom, 15)
}
}
.padding()
}
}
struct EventBar: View {
let event: Event
let selectedYear: Int
let eventCount: Int
var body: some View {
VStack(alignment: .leading) {
Rectangle()
.fill(event.color.opacity(event.startYear <= selectedYear && (event.endYear == 0 || selectedYear <= event.endYear) ? 1 : 1))
.frame(height: 20)
.overlay(
Text(event.title)
.font(.caption)
.padding(.leading, 5)
.foregroundColor(.white)
)
}
.padding(.trailing, 10)
}
}
struct EventDetailView: View {
let event: Event
var body: some View {
ScrollView {
VStack(alignment: .leading) {
Text(event.title)
.font(.title)
.padding()
// 根據事件創作的詳細描述
if event.title == "日耳曼部落時代" {
Text("日耳曼部落時代開始於公元前約500年,該時期的日耳曼部落分布在歐洲北部和中部。這些部落以農業、狩獵和戰爭為主,並逐漸與羅馬帝國接觸。")
.padding()
Image("germanic_tribes")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 300, height: 200)
.padding()
Text("在這一時期,日耳曼部落的文化和社會結構開始發展,形成了獨特的傳統和習俗。他們的社會由戰士貴族和自由農民組成,這樣的結構延續到了後來的中世紀。")
.padding()
Image("germanic_artifacts")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 300, height: 200)
.padding()
}
if event.title == "雅典的全盛時期" {
Text("雅典的全盛時期大約在公元前500年至公元前322年之間。這段時期雅典成為了古希臘文化和政治的中心,誕生了眾多偉大的哲學家和藝術家。")
.padding()
Image("athenian_acropolis")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 300, height: 200)
.padding()
Text("在這一時期,雅典建立了民主政治體制,並在建築、雕塑和哲學方面達到了新的高度。雅典衛城是這段時期的象徵,至今仍然是希臘文化的重要象徵。")
.padding()
Image("athenian_democracy")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 300, height: 200)
.padding()
}
// 添加更多事件的詳細描述和圖片
}
.navigationBarTitle(Text(event.title), displayMode: .inline)
}
}
}
struct Event: Identifiable {
let id = UUID()
let country: String
let title: String
let startYear: Int
let endYear: Int
let color: Color
let coordinate: CLLocationCoordinate2D
}
struct CustomMapAnnotation: Identifiable {
let id = UUID()
let coordinate: CLLocationCoordinate2D
let title: String?
}
struct TimelineApp_Previews: PreviewProvider {
static var previews: some View {
TimelineApp()
}
}