diff --git a/Example/HostingExample/ViewController.swift b/Example/HostingExample/ViewController.swift index 5b84729f2..f7b8e69b4 100644 --- a/Example/HostingExample/ViewController.swift +++ b/Example/HostingExample/ViewController.swift @@ -66,6 +66,6 @@ class ViewController: NSViewController { struct ContentView: View { var body: some View { - EquatableDemoView(count: 0, tag: 0) + ColorAnimationExample() } } diff --git a/Example/SharedExample/Animation/ColorAnimationExample.swift b/Example/SharedExample/Animation/ColorAnimationExample.swift new file mode 100644 index 000000000..20c4a21f7 --- /dev/null +++ b/Example/SharedExample/Animation/ColorAnimationExample.swift @@ -0,0 +1,28 @@ +// +// ColorAnimationExample.swift +// SharedExample +// +// Created by Kyle on 2025/7/20. +// +#if OPENSWIFTUI +import OpenSwiftUI +#else +import SwiftUI +#endif +import Foundation + +struct ColorAnimationExample: View { + @State private var showRed = false + var body: some View { + VStack { + Color(platformColor: showRed ? .red : .blue) + .frame(width: showRed ? 200 : 400, height: showRed ? 200 : 400) + } + .animation(.easeInOut(duration: 2), value: showRed) + .onAppear { + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + showRed.toggle() + } + } + } +} diff --git a/Example/SharedExample/Extension/Color+Platform.swift b/Example/SharedExample/Extension/Color+Platform.swift new file mode 100644 index 000000000..5b65b4cf1 --- /dev/null +++ b/Example/SharedExample/Extension/Color+Platform.swift @@ -0,0 +1,30 @@ +// +// Color+Platform.swift +// SharedExample +// +// Created by Kyle on 2025/7/20. +// + +#if OPENSWIFTUI +import OpenSwiftUI +#else +import SwiftUI +#endif + +#if os(iOS) +import UIKit +typealias PlatformColor = UIColor +#elseif os(macOS) +import AppKit +typealias PlatformColor = NSColor +#endif + +extension Color { + init(platformColor: PlatformColor) { + #if os(iOS) + self.init(uiColor: platformColor) + #elseif os(macOS) + self.init(nsColor: platformColor) + #endif + } +} diff --git a/Example/SharedExample/View/AppearanceActionModifierExample.swift b/Example/SharedExample/View/AppearanceActionModifierExample.swift index b54fc443c..a8d98f17e 100644 --- a/Example/SharedExample/View/AppearanceActionModifierExample.swift +++ b/Example/SharedExample/View/AppearanceActionModifierExample.swift @@ -12,16 +12,8 @@ import Foundation struct AppearanceActionModifierExample: View { @State private var first = true - var color: Color { - #if os(macOS) - Color(nsColor: first ? .red : .blue) - #else - Color(uiColor: first ? .red : .blue) - #endif - } - var body: some View { - color + Color(platformColor: first ? .red : .blue) .onAppear { print("View appear") DispatchQueue.main.asyncAfter(deadline: .now() + 1) { diff --git a/Example/SharedExample/View/NamespaceExample.swift b/Example/SharedExample/View/NamespaceExample.swift index 99f5408d4..9c442817f 100644 --- a/Example/SharedExample/View/NamespaceExample.swift +++ b/Example/SharedExample/View/NamespaceExample.swift @@ -13,17 +13,8 @@ struct NamespaceExample: View { @State private var first = true @Namespace private var id - var color: Color { - #if os(macOS) - Color.red - #else - - Color(uiColor: first ? .red : .blue) - #endif - } - var body: some View { - color + Color(platformColor: first ? .red : .blue) .onAppear { print("View appear \(id)") DispatchQueue.main.asyncAfter(deadline: .now() + 1) {