SwiftUI知识

SwiftUI

闲时

Ask Apple 2022 与 SwiftUI 有关的问答(上)

Simple Swift Guide

Learn to develop beautiful iOS apps with SwiftUI

SwiftLee: A weekly blog about Swift, iOS and Xcode Tips and Tricks

SerialCoder.dev: providing educational content on iOS & macOS programming

iOS Example

Cindori Blog

SafeArea

掌握 SwiftUI 的 Safe Area

1
2
3
4
5
6
7
8
// 只扩展到底部
.ignoresSafeArea(edges: .bottom)

// 扩展到顶部和底部
.ignoresSafeArea(edges: [.bottom, .trailing])

// 横向扩展
.ignoresSafeArea(edges:.horizontal)

使用Core Data存储自定义类的数组

Store an Array of Custom Data Types in Core Data With Transformable and NSSecureCoding

首先NSObject、NSSecureCoding

然后Transformable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 为了能让CoreData能够自己解析编码[Tag],首先上面的Tag需要先conform to:NSSexureCoding和NSObject
class TagAttributeTransformer: NSSecureUnarchiveFromDataTransformer {
override static var allowedTopLevelClasses: [AnyClass] {
[Tag.self]
}

static func register() {
let className = String(describing: TagAttributeTransformer.self)
let name = NSValueTransformerName(className)
let transformer = TagAttributeTransformer()

ValueTransformer.setValueTransformer(transformer, forName: name)
}
}

haptic feedback

1、2、3

UINotificationFeedbackGenerator

UIImpactFeedbackGenerator

UISelectionFeedbackGenerator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
switch i {
case 1:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)

case 2:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)

case 3:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)

case 4:
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()

case 5:
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()

case 6:
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()

default:
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
i = 0
}

4、CoreHaptics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import CoreHaptics

func complexSuccess() {
// make sure that the device supports haptics
guard CHHapticEngine.capabilitiesForHardware().supportsHaptics else { return }
var events = [CHHapticEvent]()

// create one intense, sharp tap
let intensity = CHHapticEventParameter(parameterID: .hapticIntensity, value: 1)
let sharpness = CHHapticEventParameter(parameterID: .hapticSharpness, value: 1)
let event = CHHapticEvent(eventType: .hapticTransient, parameters: [intensity, sharpness], relativeTime: 0)
events.append(event)

// convert those events into a pattern and play it immediately
do {
let pattern = try CHHapticPattern(events: events, parameters: [])
let player = try engine?.makePlayer(with: pattern)
try player?.start(atTime: 0)
} catch {
print("Failed to play pattern: \(error.localizedDescription).")
}
}

RichTextEditor

kyle-n/HighlightedTextEditor

danielsaidi/RichTextKit

cjwirth/RichEditorView

Author: Jcwang

Permalink: http://example.com/2022/11/06/SwiftUI%E7%9F%A5%E8%AF%86/