|
| 1 | +import SwiftUI |
| 2 | +@_spi(Experimental) import MapboxMaps |
| 3 | + |
| 4 | +/// Example demonstrating accessibility scaling for map symbols. |
| 5 | +/// |
| 6 | +/// This example shows three modes: |
| 7 | +/// - Fixed: Manual scale control with slider (no system listeners) |
| 8 | +/// - System: Automatic scaling based on system text size (opt-in) |
| 9 | +/// - Custom: System scaling with custom mapping that dampens large accessibility scales |
| 10 | +struct AccessibilityScaleExample: View { |
| 11 | + enum ScaleMode: String, CaseIterable { |
| 12 | + case fixed = "Fixed" |
| 13 | + case system = "System" |
| 14 | + case custom = "Custom" |
| 15 | + } |
| 16 | + |
| 17 | + @State private var selectedMode: ScaleMode = .fixed |
| 18 | + @State private var customScaleValue: Float = 1.0 |
| 19 | + |
| 20 | + var body: some View { |
| 21 | + MapReader { mapProxy in |
| 22 | + Map(initialViewport: .camera(center: .init(latitude: 40.7128, longitude: -74.0060), zoom: 12)) |
| 23 | + .mapStyle(.standard) |
| 24 | + .onMapLoaded { _ in |
| 25 | + applySymbolScaleBehavior(to: mapProxy.map) |
| 26 | + } |
| 27 | + .onChange(of: selectedMode) { _ in |
| 28 | + applySymbolScaleBehavior(to: mapProxy.map) |
| 29 | + } |
| 30 | + .onChange(of: customScaleValue) { _ in |
| 31 | + if selectedMode == .fixed { |
| 32 | + applySymbolScaleBehavior(to: mapProxy.map) |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + .ignoresSafeArea(edges: .bottom) |
| 37 | + .overlay(alignment: .bottom) { |
| 38 | + VStack(spacing: 12) { |
| 39 | + // Conditional content (above buttons) |
| 40 | + Group { |
| 41 | + if selectedMode == .fixed { |
| 42 | + VStack(spacing: 8) { |
| 43 | + HStack { |
| 44 | + Text("Scale factor:") |
| 45 | + .font(.caption) |
| 46 | + Spacer() |
| 47 | + Text(String(format: "%.2fx", customScaleValue)) |
| 48 | + .font(.caption.monospacedDigit()) |
| 49 | + } |
| 50 | + Slider(value: $customScaleValue, in: 0.8...2.0, step: 0.1) |
| 51 | + Text("Manual scale control (no system listeners)") |
| 52 | + .font(.caption2) |
| 53 | + .foregroundStyle(.secondary) |
| 54 | + .multilineTextAlignment(.center) |
| 55 | + } |
| 56 | + .padding(.horizontal, 16) |
| 57 | + .padding(.vertical, 8) |
| 58 | + .background(.ultraThinMaterial) |
| 59 | + .cornerRadius(8) |
| 60 | + .frame(maxWidth: 340) |
| 61 | + } else if selectedMode == .system { |
| 62 | + Text("Automatic scaling with default mapping.\nChange system text size in Settings to see effect.") |
| 63 | + .font(.caption2) |
| 64 | + .foregroundStyle(.secondary) |
| 65 | + .multilineTextAlignment(.center) |
| 66 | + .padding(.horizontal, 16) |
| 67 | + .padding(.vertical, 8) |
| 68 | + .background(.ultraThinMaterial) |
| 69 | + .cornerRadius(8) |
| 70 | + .frame(maxWidth: 340) |
| 71 | + } else if selectedMode == .custom { |
| 72 | + Text("Custom mapping: boosts small scales +10%, dampens large scales.\nChange system text size in Settings to see effect.") |
| 73 | + .font(.caption2) |
| 74 | + .foregroundStyle(.secondary) |
| 75 | + .multilineTextAlignment(.center) |
| 76 | + .padding(.horizontal, 16) |
| 77 | + .padding(.vertical, 8) |
| 78 | + .background(.ultraThinMaterial) |
| 79 | + .cornerRadius(8) |
| 80 | + .frame(maxWidth: 340) |
| 81 | + } |
| 82 | + } |
| 83 | + .frame(height: selectedMode == .fixed ? 80 : 60) |
| 84 | + .opacity(1) |
| 85 | + |
| 86 | + // Mode picker |
| 87 | + Picker("Scale Mode", selection: $selectedMode) { |
| 88 | + ForEach(ScaleMode.allCases, id: \.self) { mode in |
| 89 | + Text(mode.rawValue).tag(mode) |
| 90 | + } |
| 91 | + } |
| 92 | + .pickerStyle(.segmented) |
| 93 | + .frame(maxWidth: 340) |
| 94 | + } |
| 95 | + .padding(.bottom, 70) |
| 96 | + .animation(.easeInOut(duration: 0.2), value: selectedMode) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private func applySymbolScaleBehavior(to mapboxMap: MapboxMap?) { |
| 101 | + guard let mapboxMap = mapboxMap else { return } |
| 102 | + let behavior = scaleBehaviorForMode() |
| 103 | + mapboxMap.symbolScaleBehavior = behavior |
| 104 | + } |
| 105 | + |
| 106 | + private func scaleBehaviorForMode() -> SymbolScaleBehavior { |
| 107 | + switch selectedMode { |
| 108 | + case .fixed: |
| 109 | + // Fixed scale: manual control via slider |
| 110 | + return .fixed(scaleFactor: Double(customScaleValue)) |
| 111 | + case .system: |
| 112 | + // System: automatic scaling with default mapping |
| 113 | + return .system |
| 114 | + case .custom: |
| 115 | + // Custom: increases low scale values proportionally, dampens large accessibility scales |
| 116 | + return .system(mapping: { systemScale in |
| 117 | + switch systemScale { |
| 118 | + case ..<1.0: |
| 119 | + return systemScale * 1.1 // Boost small scales by 10% |
| 120 | + case 1.0...1.3: |
| 121 | + return systemScale // Keep medium scales unchanged |
| 122 | + default: |
| 123 | + return 1.3 + (systemScale - 1.3) * 0.4 // Dampen large scales (max ~1.6x) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +#Preview { |
| 131 | + AccessibilityScaleExample() |
| 132 | +} |
0 commit comments