SPB Git

Repository Public

Motion.swift 5.5 KB 144 lines swift

1//2//  Auto-Ka iOS v23//  File: AutoKa/Design/Motion.swift4//  Description: mouvement de l'interface — entrées échelonnées, sélecteur à indicateur glissant, cœur qui5//               rebondit, déploiement des graphiques. Tout respecte « Réduire les animations » et le réglage6//               Animations de l'app (« Aucune » coupe aussi ces mouvements).7//8//  Author: Simon-Pierre Boucher9//  Contact: contact@spboucher.ai10//1112import SwiftUI1314/// Le mouvement est-il permis ? (accessibilité du système et réglage de l'app)15struct MotionAllowed: DynamicProperty {16    @Environment(\.accessibilityReduceMotion) private var reduceMotion17    @Environment(BoardingCenter.self) private var door18    @MainActor var value: Bool {19        if reduceMotion { return false }20        return door.preference != .off21    }22}2324/// Entrée échelonnée au premier affichage : les éléments d'une liste se posent l'un après l'autre.25private struct StaggeredAppear: ViewModifier {26    let index: Int27    let allowed = MotionAllowed()28    @State private var shown = false2930    func body(content: Content) -> some View {31        content32            .opacity(shown || !allowed.value ? 1 : 0)33            .offset(y: shown || !allowed.value ? 0 : 12)34            .onAppear {35                guard !shown else { return }36                // seuls les premiers éléments sont décalés : la suite d'une longue liste arrive sans attente37                withAnimation(.spring(response: 0.42, dampingFraction: 0.86).delay(Double(min(index, 7)) * 0.045)) { shown = true }38            }39    }40}4142/// Progression 0 → 1 jouée une fois à l'apparition (graphiques qui se tracent, radar qui se déploie).43private struct DrawOnAppear: ViewModifier {44    @Binding var progress: Double45    var duration: Double46    var delay: Double47    let allowed = MotionAllowed()4849    func body(content: Content) -> some View {50        content.onAppear {51            guard progress < 1 else { return }52            if allowed.value {53                withAnimation(.timingCurve(0.2, 0.8, 0.2, 1, duration: duration).delay(delay)) { progress = 1 }54            } else {55                progress = 156            }57        }58    }59}6061extension View {62    func staggered(_ index: Int) -> some View { modifier(StaggeredAppear(index: index)) }63    func drawOnAppear(_ progress: Binding<Double>, duration: Double = 0.7, delay: Double = 0.05) -> some View {64        modifier(DrawOnAppear(progress: progress, duration: duration, delay: delay))65    }66}6768/// Sélecteur segmenté maison : l'indicateur glisse d'une option à l'autre (matchedGeometry).69struct SlidingSegmented<T: Hashable>: View {70    @Binding var selection: T71    let options: [(value: T, label: String)]72    var label: String73    @Namespace private var ns74    let allowed = MotionAllowed()7576    var body: some View {77        HStack(spacing: 2) {78            ForEach(options, id: \.value) { o in79                let active = o.value == selection80                Button {81                    Haptics.select()82                    withAnimation(allowed.value ? .spring(response: 0.3, dampingFraction: 0.82) : nil) { selection = o.value }83                } label: {84                    Text(o.label)85                        .font(.footnote.weight(.semibold))86                        .foregroundStyle(active ? Theme.ink : Theme.ink3)87                        .padding(.horizontal, 12)88                        .frame(minHeight: 32)89                        .background {90                            if active {91                                RoundedRectangle(cornerRadius: 7, style: .continuous).fill(Theme.surface)92                                    .shadow(color: .black.opacity(0.1), radius: 2, y: 1)93                                    .matchedGeometryEffect(id: "indicateur", in: ns)94                            }95                        }96                        .contentShape(Rectangle())97                }98                .buttonStyle(.plain)99                .accessibilityAddTraits(active ? .isSelected : [])100            }101        }102        .padding(3)103        .background(Theme.surface2, in: RoundedRectangle(cornerRadius: Theme.radiusControl, style: .continuous))104        .accessibilityElement(children: .contain)105        .accessibilityLabel(label)106    }107}108109/// Cœur des favoris : il rebondit (0,82 → 1,18 → 1 en 420 ms) et un anneau pétrole se dissipe.110struct HeartBurst: ViewModifier {111    let active: Bool112    let allowed = MotionAllowed()113    @State private var ring = false114115    func body(content: Content) -> some View {116        let moves = allowed.value117        return content118            .keyframeAnimator(initialValue: 1.0, trigger: active) { view, scale in119                view.scaleEffect(moves ? scale : 1)120            } keyframes: { _ in121                KeyframeTrack {122                    CubicKeyframe(0.82, duration: 0.1)123                    CubicKeyframe(1.18, duration: 0.16)124                    CubicKeyframe(1.0, duration: 0.16)125                }126            }127            .background {128                Circle().strokeBorder(Theme.brand, lineWidth: 2)129                    .scaleEffect(ring ? 1.9 : 0.7)130                    .opacity(ring ? 0 : (active && allowed.value ? 0.9 : 0))131                    .allowsHitTesting(false)132            }133            .onChange(of: active) { _, now in134                guard now, allowed.value else { ring = false; return }135                ring = false136                withAnimation(.easeOut(duration: 0.45)) { ring = true }137            }138    }139}140141extension View {142    func heartBurst(_ active: Bool) -> some View { modifier(HeartBurst(active: active)) }143}144
Files main
Menu

Theme