Skip to content

Commit fd54a29

Browse files
committed
Add "Creating a macOS App"
1 parent 540e18f commit fd54a29

File tree

24 files changed

+1493
-8
lines changed

24 files changed

+1493
-8
lines changed

Framework-Integration/Models/Landmark.swift

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct Landmark: Hashable, Codable, Identifiable {
1818
var category: Category
1919
var isFavorite: Bool
2020
var isFeatured: Bool
21+
var description: String
2122

2223
var locationCoordinate: CLLocationCoordinate2D {
2324
CLLocationCoordinate2D(

Framework-Integration/Resources/landmarkData.json

+12
Large diffs are not rendered by default.

Framework-Integration/Supporting Views/CircleImage.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import SwiftUI
99

1010
struct CircleImage: View {
1111
var image: Image
12+
var shadowRadius: CGFloat = 10
1213

1314
var body: some View {
1415
image
1516
.clipShape(Circle())
1617
.overlay(Circle().stroke(Color.white, lineWidth: 4))
17-
.shadow(radius: 10)
18+
.shadow(radius: shadowRadius)
1819
}
1920
}
2021

Framework-Integration/Supporting Views/MapView.swift

+29-3
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,46 @@ A view that hosts an `MKMapView`.
88
import SwiftUI
99
import MapKit
1010

11-
struct MapView: UIViewRepresentable {
11+
struct MapView {
1212
var coordinate: CLLocationCoordinate2D
1313

14-
func makeUIView(context: Context) -> MKMapView {
14+
func makeMapView() -> MKMapView {
1515
MKMapView(frame: .zero)
1616
}
1717

18-
func updateUIView(_ view: MKMapView, context: Context) {
18+
func updateMapView(_ view: MKMapView, context: Context) {
1919
let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
2020
let region = MKCoordinateRegion(center: coordinate, span: span)
2121
view.setRegion(region, animated: true)
2222
}
2323
}
2424

25+
#if os(macOS)
26+
27+
extension MapView: NSViewRepresentable {
28+
func makeNSView(context: Context) -> MKMapView {
29+
makeMapView()
30+
}
31+
32+
func updateNSView(_ nsView: MKMapView, context: Context) {
33+
updateMapView(nsView, context: context)
34+
}
35+
}
36+
37+
#else
38+
39+
extension MapView: UIViewRepresentable {
40+
func makeUIView(context: Context) -> MKMapView {
41+
makeMapView()
42+
}
43+
44+
func updateUIView(_ uiView: MKMapView, context: Context) {
45+
updateMapView(uiView, context: context)
46+
}
47+
}
48+
49+
#endif
50+
2551
struct MapView_Previews: PreviewProvider {
2652
static var previews: some View {
2753
MapView(coordinate: landmarkData[0].locationCoordinate)

MacLandmarks/AppDelegate.swift

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// AppDelegate.swift
3+
// MacLandmarks
4+
//
5+
// Created by 王炜 on 2020/2/6.
6+
//
7+
8+
import Cocoa
9+
import SwiftUI
10+
11+
@NSApplicationMain
12+
class AppDelegate: NSObject, NSApplicationDelegate {
13+
14+
var window: NSWindow!
15+
16+
17+
func applicationDidFinishLaunching(_ aNotification: Notification) {
18+
// Create the SwiftUI view that provides the window contents.
19+
let contentView = ContentView().environmentObject(UserData())
20+
21+
// Create the window and set the content view.
22+
window = NSWindow(
23+
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
24+
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
25+
backing: .buffered, defer: false)
26+
window.center()
27+
window.setFrameAutosaveName("Main Window")
28+
window.contentView = NSHostingView(rootView: contentView)
29+
window.makeKeyAndOrderFront(nil)
30+
}
31+
32+
func applicationWillTerminate(_ aNotification: Notification) {
33+
// Insert code here to tear down your application
34+
}
35+
36+
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "mac",
5+
"size" : "16x16",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "mac",
10+
"size" : "16x16",
11+
"scale" : "2x"
12+
},
13+
{
14+
"idiom" : "mac",
15+
"size" : "32x32",
16+
"scale" : "1x"
17+
},
18+
{
19+
"idiom" : "mac",
20+
"size" : "32x32",
21+
"scale" : "2x"
22+
},
23+
{
24+
"idiom" : "mac",
25+
"size" : "128x128",
26+
"scale" : "1x"
27+
},
28+
{
29+
"idiom" : "mac",
30+
"size" : "128x128",
31+
"scale" : "2x"
32+
},
33+
{
34+
"idiom" : "mac",
35+
"size" : "256x256",
36+
"scale" : "1x"
37+
},
38+
{
39+
"idiom" : "mac",
40+
"size" : "256x256",
41+
"scale" : "2x"
42+
},
43+
{
44+
"idiom" : "mac",
45+
"size" : "512x512",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "mac",
50+
"size" : "512x512",
51+
"scale" : "2x"
52+
}
53+
],
54+
"info" : {
55+
"version" : 1,
56+
"author" : "xcode"
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"filename" : "star-empty.pdf",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"filename" : "star-filled.pdf",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}
Binary file not shown.

0 commit comments

Comments
 (0)