Hello,
I'm writing an EntityAction that animates a material base tint between two different colours. However, the colour that is being actually set differs in RGB values from that requested.
For example, trying to set an end target of R0.5, G0.5, B0.5, results in a value of R0.735357, G0.735357, B0.735357. I can also see during the animation cycle that intermediate actual tint values are also incorrect, versus those being set.
My understanding is the the values of material base colour are passed as a SIMD4. Therefore I have a couple of helper extensions to convert a UIColor into this format and mix between two colours. Note however, I don't think the issue is with this functions - even if their outputs are wrong, the final value of the base tint doesn't match the value being set.
I wondered if this was a colour space issue?
import simd
import RealityKit
import UIKit
typealias Float4 = SIMD4<Float>
extension Float4 {
func mixedWith(_ value: Float4, by mix: Float) -> Float4 {
Float4(
simd_mix(x, value.x, mix),
simd_mix(y, value.y, mix),
simd_mix(z, value.z, mix),
simd_mix(w, value.w, mix)
)
}
}
extension UIColor {
var float4: Float4 {
var r: CGFloat = 0.0
var g: CGFloat = 0.0
var b: CGFloat = 0.0
var a: CGFloat = 0.0
getRed(&r, green: &g, blue: &b, alpha: &a)
return Float4(Float(r), Float(g), Float(b), Float(a))
}
}
struct ColourAction: EntityAction {
let startColour: SIMD4<Float>
let targetColour: SIMD4<Float>
var animatedValueType: (any AnimatableData.Type)? { SIMD4<Float>.self }
init(startColour: UIColor, targetColour: UIColor) {
self.startColour = startColour.float4
self.targetColour = targetColour.float4
}
static func registerEntityAction() {
ColourAction.subscribe(to: .updated) { event in
guard let animationState = event.animationState else { return }
let interpolatedColour = event.action.startColour.mixedWith(event.action.targetColour, by: Float(animationState.normalizedTime))
animationState.storeAnimatedValue(interpolatedColour)
}
}
}
extension Entity {
func updateColour(from currentColour: UIColor, to targetColour: UIColor, duration: Double, endAction: @escaping (Entity) -> Void = { _ in }) {
let colourAction = ColourAction(startColour: currentColour, targetColour: targetColour, endedAction: endAction)
if let colourAnimation = try? AnimationResource.makeActionAnimation(for: colourAction, duration: duration, bindTarget: .material(0).baseColorTint) {
playAnimation(colourAnimation)
}
}
}
The EntityAction can only be applied to an entity with a ModelComponent (because of the material), so it can be called like so:
guard
let modelComponent = entity.components[ModelComponent.self],
let material = modelComponent.materials.first as? PhysicallyBasedMaterial else
{
return
}
let currentColour = material.baseColor.tint
let targetColour = UIColor(_colorLiteralRed: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)
entity.updateColour(from:currentColour, to: targetColour, duration: 2)
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I have a scene built up in RealityComposerPro, in which I've added a ParticleEmitter with isEmitting set to False and 'Loop' set to True.
In my app, when I toggle isEmitting to True there can be a delay of a few seconds before the ParticleEmitter starts.
However, if I programatically add the emitter in code at that point, it starts immediately.
To be clear, I'm seeing this on the VisionOS simulator - I don't have access to a device at this time.
Am I misunderstanding how to control the ParticleEmitter when I need precise control on when it starts.
As I understand it there are two ways I can track a hand, or a joint, in RealityKit:
either, create an AnchorEntity, for example AnchorEntity(.hand(.left, location: .palm))
or, set up an ARSession with a HandTrackingProvider ( a lot more code which I haven't repeated here).
Assuming this is correct, when would I want to use one over the other?
In ARKit for visionOS, I can track the user's head with a HeadAnchor, but it will not give the location. However, I can get the device's transform by calling queryDeviceAnchor(atTimestamp: CACurrentMediaTime()) on a WorldTrackingProvider.
Why the difference? - if I know the device's transform, I effectively know the head's transform.
Had anyone experienced convexCast causing a crash and what might be behind it?
Here's the call stack:
Context
I'm writing an app that allows a user to select folders, and then scan them for files. The folder could be on the user's machine, iCloud or external storage. The app persists the details of these files and folders to a document, so the user can access the files or re-scan the folders in the future. The App is being written using the SwiftUI framework for MacOS and the iPad.
Problems
Given this is a Sandboxed app I need to create security-scoped bookmarks to be able to access the files and folders that have been persisted to the document. I have two questions:
How can I create a document-scoped bookmark, when using ReferenceFileDocument protocol. I need the document's URL, but I will never have access to this as I'm using the ReferenceFileDocument. I want to achieve something like this:
func fileWrapper(snapshot: MyDocument, configuration: WriteConfiguration) throws - FileWrapper {
:
let bookmarkDataToPersist = snapshort.sourceFolderURL.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: documentURL)
:
}
2. Ideally the user would be able to:
* connect an external drive to their Mac
* select a folder on that drive
* save the document, which would persist a bookmark to that folder's URL
* send the document to an iPad (via email or iCloud drive)
* open the document using the iPad version of the App
* connect the external bookmark to the iPad
* re-scan the folder which was book marked in the document from the folder
But given the problem in 1) and the fact that document-bookmarks cannot point to folders, is there a way?
Any ideas or suggestions would be very welcome
How do you add a button in the toolbar to hide/show the sidebar?
The following code is stripped out from an app that compiles and works under MacOS 11. It shows a tab bar - clicking on an item will highlight it. Under the tab bar is a text edit field where one can edit the title of the selected tab.
There are 2 issues:
The tab title shown in the title editor is allows out of phase
from the selected tab
Irrespective of which tab item is selected, when the title is edited
it always updates the first tab
I'm not sure if I was very lucky that this code ever worked under Big Sur, or if there is an issue with Monterey. I'm definitely not holding this up as example code - I'm sure there are better ways to implement it, but seek opinions on whether it should work.
import SwiftUI
class Document: ObservableObject {
var tabs = [Tab(id: 0), Tab(id: 1)]
var viewSettings = ViewSettings()
}
class Tab: ObservableObject, Identifiable {
let id: Int
@Published var title: String
init(id: Int) {
self.id = id
self.title = "Tab \(id)"
}
}
class ViewSettings: ObservableObject {
@Published var activeTab: Int = 0
}
@main
struct Test: App {
@StateObject var document: Document = Document()
var body: some Scene {
WindowGroup {
ContentView()
.padding()
.environmentObject(document)
.environmentObject(document.viewSettings)
}
}
}
struct ContentView: View {
@EnvironmentObject var document: Document
@EnvironmentObject var viewSettings: ViewSettings
var body: some View {
TabBar(viewSettings: document.viewSettings)
TabEditView(activeTab: document.tabs[viewSettings.activeTab])
}
}
struct TabEditView: View {
@EnvironmentObject var viewSettings: ViewSettings
@ObservedObject var activeTab: Tab
@State var title: String = ""
init(activeTab: Tab) {
print("CONSOLE - Init TabEditView for tab \(activeTab.id)")
self.activeTab = activeTab
}
var body: some View {
HStack {
Text("Tab title:")
TextField("Tab title:", text: $title, onCommit: { activeTab.title = title })
.onAppear { title = activeTab.title }
.onChange(of: viewSettings.activeTab) { _ in
print("CONSOLE - Updating TextField from tab \(activeTab.id)")
title = activeTab.title
}
}
}
}
struct TabBar: View {
@EnvironmentObject var document: Document
@ObservedObject var viewSettings: ViewSettings
var body: some View {
HStack {
ForEach(document.tabs, content: TabItem.init)
}
}
}
struct TabItem: View {
@EnvironmentObject var viewSettings: ViewSettings
@ObservedObject var tab: Tab
init(_ tab : Tab) { self.tab = tab }
var body: some View {
Text(tab.title)
.padding(2)
.background(tab.id == viewSettings.activeTab ? .red : .clear)
.cornerRadius(4)
.onTapGesture {
viewSettings.activeTab = tab.id
}
}
}
I wanted to know if I am doing something fundamentally wrong with Xcode 13 beta 3, with respect to localisation.
I'm using the new compiler support to extract strings for localisation then exporting -> translating -> importing via the .xcloc files. All this works nicely, aside from one problem...
I'm having difficulty getting it to create the initial Localizable.strings file in the development language, i.e. en in my case. When I try and import the relevant en.xcloc file nothing happens. I can import other languages without problem.
Here are the steps I am taking:
Write some basic code with one piece of text that can be localised
Select Product -> Export Localisations... and choose English - Development Language
Correctly creates en.xcloc file - contents as expected, picking up the text to be localised.
Select Product -> Import Localisations... and re-import the english en.xcloc file
This does nothing. So I try the following:
Go to Project's file -> Info tab and add a new language, say (Fr)
Select Product -> Export Localisations... and choose English - Development Language and French options
Open fr.xcloc file and add translations required
Come back to Xcode and import fr.xcloc file
Now the Localizable.strings file appears - it is of course the French version
Importing the en.xcloc file still does nothing
If I open up the file inspector on the right, I can click to add an English version, but it simply copies the French version (which causes lots of other issues)...
Any ideas?
How can one conform an actor to the Sequence protocol? The following code generates the compiler warning: Instance method 'makeIterator()' isolated to global actor 'MainActor' can not satisfy corresponding requirement from protocol 'Sequence'.
@MainActor class Test: Sequence {
private var contents: [Int] = []
func makeIterator() -> Array<Int>.Iterator {
contents.makeIterator()
}
}
I'm trying to figure out the correct structure for a macOS document app using SwiftUI and Swift 5.5 concurrency features.
I want to demonstrate updating a document's data asynchronously, in a thread safe manner, with the ability to read / write the data to a file, also thread-safe and in the background. Yet I am struggling to:
write clean code - some of it looks inelegant at best, more like clunky, compared to my prior apps which used DispatchQueues etc
implement Codeable conformance for an actor
I'm seeking ideas, corrections and advice on how to improve on this. I've posted the full code over at GitHub, as I will only highlight some particular elements here. This is a minimum viable app, just for proof-of-concept purposes.
The app
The app displays a list of Records with a button to add more. It should be able to save and reload the list from a file.
Current approach / design
I've chosen the ReferenceFileDocument protocol for the Document type, as this is what I would use in a future app which has a more complex data structure. (i.e. I'm not planning on using a pure set of structs to hold a documents' data)
Document has a property content of type RecordsModelView representing the top-level data structure.
RecordsModelView is annotated with @MainActor to ensure any updates it receives will be processed on the main thread.
RecordsModelView has a property of type RecordsModel. This is an actor ensuring read/write of its array of Records are thread safe, but not coordinated via the MainActor for efficiency.
The app assumes that the func to add an item takes a long time, and hence runs it from with a Task. Although not demonstrated here, I am also making the assumption that addRecord maybe called from multiple background threads, so needs to be thread safe, hence the use of an actor.
The code compiles and runs allowing new items to be added to the list but...
Issues
Firstly, I can't annotate Document with @MainActor - generates compiler errors I cannot resolve. If I could I think it might solve some of my issues...
Secondly, I therefore have a clunky way for Document to initialise its content property (which also has to be optional to make it work). This looks nasty, and has the knock on effect of needing to unwrap it everywhere it is referenced:
final class Document: ReferenceFileDocument {
@Published var content: RecordsViewModel?
init() {
Task { await MainActor.run { self.content = RecordsViewModel() } }
}
// Other code here
}
Finally, I can't get the RecordsModel to conform to Encodable. I've tried making encode(to encoder: Encoder) async, but this does not resolve the issue. At present, therefore RecordsModel is just conformed to Decodable.
func encode(to encoder: Encoder) async throws { // <-- Actor-isolated instance method 'encode(to:)' cannot be used to satisfy a protocol requirement
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(records, forKey: .records)
}
I have encountered an issue when trying to update the status of a detached task, by passing a closure to MainActor.run.
To illustrate the issue consider a function that counts the number of files in a folder and its sub-directories. It runs in a Task.detached closure, as I don't want it blocking the main thread. Every 10,000th file it updates a Published property fileCount, by passing a closure to MainThread.run.
However, the UI is failing to update and even giving me a spinning beach ball. The only way to stop this is by inserting await Task.sleep(1_000_000_000) before the call to MainThread.run. Here's the code:
final class NewFileCounter: ObservableObject {
@Published var fileCount = 0
func findImagesInFolder(_ folderURL: URL) {
let fileManager = FileManager.default
Task.detached {
var foundFileCount = 0
let options = FileManager.DirectoryEnumerationOptions(arrayLiteral: [.skipsHiddenFiles, .skipsPackageDescendants])
if let enumerator = fileManager.enumerator(at: folderURL, includingPropertiesForKeys: [], options: options) {
while let _ = enumerator.nextObject() as? URL {
foundFileCount += 1
if foundFileCount % 10_000 == 0 {
let fileCount = foundFileCount
await Task.sleep(1_000_000_000) // <-- Only works with this in...comment out to see failure
await MainActor.run { self.fileCount = fileCount }
}
}
let fileCount = foundFileCount
await MainActor.run { self.fileCount = fileCount }
}
}
}
}
The code works if I revert to the old way of achieving this:
final class OldFileCounter: ObservableObject {
@Published var fileCount = 0
func findImagesInFolder(_ folderURL: URL) {
let fileManager = FileManager.default
DispatchQueue.global(qos: .userInitiated).async {
let options = FileManager.DirectoryEnumerationOptions(arrayLiteral: [.skipsHiddenFiles, .skipsPackageDescendants])
var foundFileCount = 0
if let enumerator = fileManager.enumerator(at: folderURL, includingPropertiesForKeys: [], options: options) {
while let _ = enumerator.nextObject() as? URL {
foundFileCount += 1
if foundFileCount % 10_000 == 0 {
let fileCount = foundFileCount
DispatchQueue.main.async { self.fileCount = fileCount }
}
}
let fileCount = foundFileCount
DispatchQueue.main.async { self.fileCount = fileCount }
}
}
}
}
What am I doing wrong?
BTW - if you want to try out this code, here is a test harness. Be sure to pick a folder with lots of files in it and its sub-folders.
import SwiftUI
@main
struct TestFileCounterApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State private var showPickerOld = false
@StateObject private var fileListerOld = OldFileCounter()
@State private var showPickerNew = false
@StateObject private var fileListerNew = NewFileCounter()
var body: some View {
VStack {
Button("Select folder to count files using DispatchQueue...") { showPickerOld = true }
Text("\(fileListerOld.fileCount)").foregroundColor(.green)
.fileImporter(isPresented: $showPickerOld, allowedContentTypes: [.folder], onCompletion: processOldSelectedURL )
Divider()
Button("Select folder to count files using Swift 5.5 concurrency...") { showPickerNew = true }
Text("\(fileListerNew.fileCount)").foregroundColor(.green)
.fileImporter(isPresented: $showPickerNew, allowedContentTypes: [.folder], onCompletion: processNewSelectedURL )
}
.frame(width: 400, height: 130)
}
private func processOldSelectedURL(_ result: Result<URL, Error>) {
switch result {
case .success(let url): fileListerOld.findImagesInFolder(url)
case .failure: return
}
}
private func processNewSelectedURL(_ result: Result<URL, Error>) {
switch result {
case .success(let url): fileListerNew.findImagesInFolder(url)
case .failure: return
}
}
}```
I was recently looking at Apple's sample code for a ReferenceFileDocument based SwiftUI document app: Building a Document-Based App with SwiftUI
The two main data types, Checklist and ChecklistItem, are defined as structs. There is also a helper struct BindingCollection that converts a binding to a collection of elements into a collection of bindings to the individual elements. The app relies on bindings to these structs.
However, in some ways, isn't a binding to a struct circumnavigating value-type semantics? Any piece of code that is passed the Binding can alter the value, rather than a copy of the value? How does SwiftUI know that the struct has been updated without a publisher?
Last question: if I'd been writing this app myself, I would have made Checklist a class, conforming to ObservableObject publishing its items property. This would have avoided the helper function, and use of bindings. What are the benefits of the struct / binding approach in the example code?
I am writing an App with both Mac and iPad versions that saves its state to a file. The user can store the file in their iCloud and open it both on the Mac and iPad simultaneously. If an update is made on either device the other will update their state to match.
This has been achieved using an NSFilePresenter / NSFileCoordinator approach. Both platforms are using identical code.
At present when I make a change on the iPad, a few moments later the Mac updates as expected.
However, this does not happen the other way around. If I update the data on the Mac the iPad never appears to receive an update.
I've tried this on the simulator also. I get similar behaviour, but can get the iPad to update it I use the simulator's Sync With iCloud function.
Any ideas of what I may be doing wrong - as clearly the code works in one direction!
Topic:
App & System Services
SubTopic:
General
Tags:
Files and Storage
Swift
Foundation
iCloud Drive
The XCODE TSAN thread analyser is throwing up a threading issue:
Data race in generic specialization <Foundation.UUID> of Swift._NativeSet.insertNew(_: __owned τ_0_0, at: Swift._HashTable.Bucket, isUnique: Swift.Bool) -> () at 0x10a16b300
This only occurs in a release build, and its Data race in generic specialization that has my attention.
It pin-points the addID function. But I cannot see the issue. Here is the relevant code snippet:
final class IDBox {
let syncQueue = DispatchQueue(label: "IDBox\(UUID().uuidString)", attributes: .concurrent)
private var _box: Set<UUID>
init() {
self._box = []
}
var box: Set<UUID> {
syncQueue.sync {
self._box
}
}
func addID(_ id: UUID) {
syncQueue.async(flags: .barrier) {
self._box.insert(id)
}
}
}