One line of code
to implement interfaces of UIKit,AppKit,and WatchKit
in SwiftUI
interface!
Docs
)The basic idea of InterfaceKit is that we want some user interface abstraction layer that sufficiently encapsulates actually calling UIKit,AppKit,and WatchKit directly. It should be simple enough that common things are easy, but comprehensive enough that complicated things are also easy.
You can check out more about the project direction in the vision document.
┌──────────────┐
│ SwiftUI │
└──────▲───────┘
│
┌──────────────────────────┴───────────────────────────┐
│ InterfaceKit │
└───────▲──────────────────▲───────────────────▲───────┘
│ │ │
┌───────┴──────┐ ┌──────┴───────┐ ┌──────┴───────┐
│ UIKit │ │ AppKit │ │ WatchKit │
└──────────────┘ └──────────────┘ └──────────────┘
InterfaceView(MyUIView())
InterfaceViewController(MyUIViewController())
NSInterfaceView(MyNSView())
NSInterfaceViewController(MyNSViewController())
WKInterfaceView(MyWKInterfaceObject())
import SwiftUI
import InterfaceKit
struct MyInterfaceView: View {
var body: some View {
ZStack {
InterfaceViewController(MyViewController())
InterfaceView(MyView())
SwiftUIView()
}
}
}
#if DEBUG
struct MyInterfaceView_Previews: PreviewProvider {
static var previews: some View {
MyInterfaceView()
}
}
#endif
You can do something while presenting SwiftUI view.
InterfaceViewController(MyUIViewController.shared, {
print("Hello World")
MyUIViewController.shared.delegate = SomeViewControler.shared
MyUIViewController.shared.view.alpha = 0.5
MyUIViewController.shared.view.backgroundColor = .white
MyUIViewController.someFunction()
networkRequest()
JSONParsing()
downloadFile()
showProgress()
makeToast()
databaseOperation()
//do something
...
})
.navigationBarBackButtonHidden(false)
.navigationBarHidden(false)
.navigationBarTitle(I18n.localizedString("Title"), displayMode: .large)
InterfaceKit makes it clearer for multiple platforms programming.
import SwiftUI
import MapKit
import InterfaceKit
let kStr = "Hello World"
#if os(iOS) || os(tvOS)
typealias OSViewController = UIViewController
typealias OSView = UILabel
typealias OSInterfaceView = InterfaceView
typealias OSInterfaceVC = InterfaceViewController
let kBounds = UIScreen.main.bounds as CGRect?
#elseif os(macOS)
typealias OSViewController = NSViewController
typealias OSView = NSTextField
typealias OSInterfaceView = NSInterfaceView
typealias OSInterfaceVC = NSInterfaceViewController
let kBounds = NSScreen.main?.frame
#endif
@main
struct EApp: App {
var body: some Scene {
WindowGroup {
ZStack {
#if !os(watchOS)
OSInterfaceView(MKMapView())
OSInterfaceView(MyView(), { print(kStr) })
OSInterfaceVC(MyVC())
#else
WKInterfaceView(WKInterfaceMap(), { print(kStr) })
#endif
Text(kStr).foregroundColor(.purple)
}
}
}
}
#if !os(watchOS)
class MyVC: OSViewController {
#if os(iOS) || os(tvOS)
override func viewDidLoad() {
let lbl = MyView()
lbl.textAlignment = .right
view.addSubview(lbl)
}
#elseif os(macOS)
override func loadView() { view = MyView() }
#endif
}
class MyView: OSView {
override init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: kBounds!.height / 2 - 60, width: kBounds!.width, height: 40))
#if os(iOS) || os(tvOS)
text = kStr
#elseif os(macOS)
stringValue = kStr
#endif
}
required init?(coder: NSCoder) { fatalError() }
}
#endif
This project is actively under development. We consider it ready for production use.
Below is a table that shows which version of InterfaceKit you should use for your Swift version.
Swift | InterfaceKit |
---|---|
5.X | >= 5.4.0 |
InterfaceKit supports multiple methods for installing the library in a project.
Clone the repository by running the following command:
git clone https://github.com/adong666666/InterfaceKit.git --depth=1
Copy the Swift files in InterfaceKit
folder to your project.
UIInterface.swift
.NSInterface.swift
.WKInterface.swift
.CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. If you have not installed CocoaPods, just install it with the following command:
$ gem install cocoapods
# Podfile
source ‘https://github.com/CocoaPods/Specs.git‘
# platform:ios, ‘13.0‘
use_frameworks!
inhibit_all_warnings!
target ‘YOUR_TARGET_NAME‘ do
pod ‘InterfaceKit‘
end
# post_install do |installer_representation|
# installer_representation.pods_project.targets.each do |target|
# target.build_configurations.each do |config|
# config.build_settings[‘IPHONEOS_DEPLOYMENT_TARGET‘] = ‘13.0‘
# end
# end
# end
Replace YOUR_TARGET_NAME
with your project name.
To integrate InterfaceKit into your Xcode project using CocoaPods, specify it in your Podfile
:
pod ‘InterfaceKit‘
Maybe you have not update CocoaPods, then InterfaceKit
may not be found, you can run pod update
to update CocoaPods, or just run the following command.
pod ‘InterfaceKit‘, :git => ‘https://github.com/adong666666/InterfaceKit.git‘
Podfile
:pod ‘InterfaceKit/UIKit‘
Podfile
:pod ‘InterfaceKit/AppKit‘
Podfile
:pod ‘InterfaceKit/WatchKit‘
Podfile
:pod ‘InterfaceKit‘, :git => ‘https://github.com/adong666666/InterfaceKit.git‘
specific
release of the framework, you can specify it like following in your Podfile
:pod ‘InterfaceKit‘, :git => ‘https://github.com/adong666666/InterfaceKit.git‘, :branch => ‘master‘#, commit: "b7e1facdedd8fe16d04ef5f47c4697e89bad9f27", ‘~> 5.4.0‘, :tag => ‘5.4.0‘
Then, in the Podfile
directory(Make sure that your Podfile and your xcodeproj file are in the same directory), run the following command:
$ pod install
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
Cartfile
(If you don‘t have Cartfile, just create one or use the Cartfile provided by this repository in CartfileExample
folder):github "adong666666/InterfaceKit" "master"
Cartfile
directory(Make sure that your Podfile and your xcodeproj file are in the same directory), run carthage update --use-xcframeworks
.carthage update --platform iOS
.carthage update --platform macOS
.carthage update --platform tvOS
.carthage update --platform watchOS
."General"
settings tab, in the "Frameworks,Libraries,and Embedded Content"
section, drag and drop InterfaceKit
xcframework (or just select the appropriate framework from xcframework) from the Carthage/Build folder on disk.Carthage defaults to building InterfaceKit as a Dynamic Library.
If you wish to build InterfaceKit as a Static Library using Carthage you may use the script below to manually modify the framework type before building with Carthage:
carthage update InterfaceKit --platform iOS --no-build
sed -i -e ‘s/MACH_O_TYPE = mh_dylib/MACH_O_TYPE = staticlib/g‘ Carthage/Checkouts/InterfaceKit/InterfaceKit/InterfaceKit.xcodeproj/project.pbxproj
carthage build InterfaceKit --platform iOS
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler. It is in early development, but InterfaceKit does support its use on supported platforms.
dependencies
value of your Package.swift
. Then run swift build
.dependencies: [
.package(url: "https://github.com/adong666666/InterfaceKit.git", .upToNextMajor(from: "5.4.0"))
]
OR
If you prefer not to use any of the aforementioned dependency managers, you can integrate InterfaceKit into your project manually.
cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:$ git init
$ git submodule add https://github.com/adong666666/InterfaceKit.git
Open the InterfaceKit
folder, and drag the InterfaceKit.xcodeproj
into the Project Navigator of your application‘s Xcode project.
It should appear nested underneath your application‘s blue project icon. Whether it is above or below all the other Xcode groups does not matter.
Select the InterfaceKit.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target.
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
In the tab bar at the top of that window, open the "General"
panel.
Click on the +
button under the "Frameworks,Libraries,and Embedded Content"
section.
You will see the InterfaceKit
folder under Workspace
. There are InterfaceKit.framework
and InterfaceKitTests.xctest
in the InterfaceKit
folder`
Click
InterfaceKit.framework
, and then clickAdd
And that‘s it!
The
InterfaceKit.framework
is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
InterfaceKit.xcframework.zip
file provided by the repository.“General”
settings tab, in the "Frameworks,Libraries,and Embedded Content"
section, drag and drop the unzipped file InterfaceKit.xcframework
(or just select the appropriate framework from InterfaceKit.xcframework).xcfameworks
, just drag and drop InterfaceKit.xcframework
. InterfaceKit.xcframework
supports all four platforms(iOS, macOS, tvOS, watchOS
).frameworks
, select the appropriate framework from InterfaceKit.xcframework according to the folder name and the platform of your project.One line of code
to implement interfaces of UIKit,AppKit,and WatchKit
in SwiftUI
interface! InterfaceKit supports for all four platforms(iOS, macOS, tvOS, watchOS
). InterfaceKit is constantly
updated.
InterfaceKit is owned and maintained by Saidong Zhang. You can follow him on github at @Github for project updates and releases.
If you believe you have identified a security vulnerability with InterfaceKit, you should report it as soon as possible via email to 3440217568@qq.com.
No donation required, but thanks anyway.
Hey! Do you like InterfaceKit? Awesome! We could actually really use your help!
Open source isn‘t just writing code. InterfaceKit could use your help with any of the following:
If any of that sounds cool to you, send a pull request! After your first contribution, we will add you as a member to the repo so you can merge pull requests and help steer the ship ?? You can read more details about that in our contributor guidelines.
InterfaceKit‘s community has a tremendous positive energy, and the maintainers are committed to keeping things awesome. Like in the CocoaPods community, always assume positive intent; even if a comment sounds mean-spirited, give the person the benefit of the doubt.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
If you add or remove a source file from InterfaceKIt, a corresponding change needs to be made to the InterfaceKit.xcworkspace
project at the root of this repository. This project is used for Carthage. Don‘t worry, you‘ll get an automated warning when submitting a pull request if you forget.
Whether you’re a core member or a user trying it out for the first time, you can make a valuable contribution to InterfaceKit by improving the documentation. Help us by:
git checkout -b my-new-feature
git commit -am ‘Add some feature‘
git push origin my-new-feature
InterfaceKit is released under the MIT license. See LICENSE for details.
See CHANGELOG.md for details.
原文:https://www.cnblogs.com/adong666666/p/14723734.html