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

Medium

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

【iOS】CoreData保存自定义数据(swift)

首先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] {
[NSArray.self, Tag.self]
}

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

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

Core Data Relationship in Swift 5— made simple, singer and her songs

From Hex to Color and Back in SwiftUI

有关,我在存储我的tags的时候,用的transformable存储的[Tag]数组,但是更改数组里面的一个Tag的名字或者颜色在存储的时候是无效的,原因如下,所以我后面直接用one to many这个relationship了。

Swift: How to update transformable objects

Transformable attributes are a immutable type, therefore cannot be changed. The only way of changing them is by creating a new object and saving it again to core data.

To solve this I deleted the property cartItemAttribute from my xcdatamodel which holds both a product and a quantity as one transformable attribute. I replaced it with a product attribute of type transformable and a quantity attribute of type Int and everything works fine now.

one to many之后使用自动生成的addToTags这些会有问题,所以自己写

Saving CoreData to-many relationships in Swift

Setting an NSManagedObject relationship in Swift

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
31
32
33
34
35
// MARK: Generated accessors for tags
extension PersonInfo {
public var wrappedTags: [CloudTag] {
let set = tags as? Set<CloudTag> ?? []
return set.sorted {
return $0.wrappedcreatdDate < $1.wrappedcreatdDate
}
}

public func getTag(from tagID: UUID) -> CloudTag? {
return self.wrappedTags.first { $0.wrappedID == tagID}
}

// @objc(addTagsObject:)
// @NSManaged public func addToTags(_ value: CloudTag)
//
// @objc(removeTagsObject:)
// @NSManaged public func removeFromTags(_ value: CloudTag)
//
// @objc(addTags:)
// @NSManaged public func addToTags(_ values: NSSet)
//
// @objc(removeTags:)
// @NSManaged public func removeFromTags(_ values: NSSet)

func addPersonInfoTag(value: CloudTag) {
let items = self.mutableSetValue(forKey: "tags")
items.add(value)
}

func removePersonInfoTag(value: CloudTag) {
let items = self.mutableSetValue(forKey: "tags");
items.remove(value)
}
}

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

Styling List Views

Author: Jcwang

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