Post

Replies

Boosts

Views

Activity

repeatCount in UIViewPropertyAnimator
I have a func to flash a view a number of times and execute closure at the end. The following code with UIView.setAnimationRepeatCount works OK: animation occurs the requested number of times and afterEnd closure executes at the end. func flashIt(repeated: Int, cycleTime: Double, delayed: Double = 5.0, afterEnd: (() -> Void)? = nil) { if repeated < 0 { return } let initAlpha = self.alpha UIView.animate(withDuration: cycleTime, delay: delayed, options:[.allowUserInteraction, .curveEaseInOut, .autoreverse, .repeat], animations: { UIView.setAnimationRepeatCount(Float(repeated)) self.alpha = 0.1 // Not 0.0, to allow user interaction }, completion: { (done: Bool) in self.alpha = initAlpha afterEnd?() } ) } To address UIView.setAnimationRepeatCount deprecation, I now try this (inspired by https://stackoverflow.com/questions/47496584/uiviewpropertyanimator-reverse-animation) func flashIt(repeated: Int, cycleTime: Double, delayed: Double = 5.0, afterEnd: (() -> Void)? = nil) { if repeated < 0 { return } let initAlpha = self.alpha let animator = UIViewPropertyAnimator(duration: cycleTime, curve: .linear) { self.alpha = 0.1 } animator.addCompletion { _ in let reverseAnimator = UIViewPropertyAnimator(duration: cycleTime, curve: .linear) { self.alpha = initAlpha } reverseAnimator.addCompletion { [self] _ in flashIt(repeated: repeated-1, cycleTime: cycleTime, delayed: 0) // without delay here } reverseAnimator.startAnimation() if repeated <= 1 { // 1 and not 0, otherwise an extra loop… afterEnd?() // Not called return } } animator.startAnimation(afterDelay: delayed) } The flash works, but afterEnd closure is never called. I've tried to call if repeated <= 1 { // 1 and not 0, otherwise an extra loop… afterEnd?() // Not called return } in other places, to no avail. What do I miss ? Is there a better and simpler way to have a RepeatCount with UIViewPropertyAnimator ?
3
0
1.5k
Jan ’22
What is this view that appears floating over the tableView ?
I have a tableView inside a UIView in a VC. This VC is in a navigation view. It is a UIViewController, not a UITableViewController. The VC set up is this one: The issue: When I run app, there is a bar (full width, about 25 pixels high) appearing over the tableView at the bottom, hiding partially a cell (we see the (i) button nearly completely hidden). When scrolling tableView, this bar does not move and hides other cell. I looked at the view in debugger (I edited the content of cells): and found that the bar which is hiding comes from navigation controller: So 2 questions: what is this view ? How to avoid it coming over the table view ? I have a solution by replacing upperView by a header in tableView, but I would like to understand what goes on here.
5
0
1.5k
Mar ’22
Contribution to Quinn’s Top Ten DevForums Tips
Hi Quinn, if I may add a point 10 to your (very meaningful) list: when pasting images, think of reducing the size (can edit simply the image ref by dividing a dimension by 2) to avoid huge images that clutter the screen. When you get an answer, please indicate if that solved the issue, if not explain what is not working. And don't forget to close the thread by marking the correct answer once you got it.
5
0
3.3k
Jun ’22
Add Return key subview to keyboard
That's an old question (this thread https://developer.apple.com/forums/thread/16375 dates back iOS9 or this other thread https://stackoverflow.com/questions/67249956/uisearchbar-warning-uitexteffectswindow-should-not-become-key-please-file-a-bu), but I've not found a comprehensive answer yet. I need to add a Return key to a numeric keyboard (BTW, how is it it does not exist in standard ?) I used to do it using let keyBoardWindow = UIApplication.shared.windows.last  to get the keyboard window and then adding the subview (self.returnButton) to it keyBoardWindow?.addSubview(self.returnButton) Worked great and still works OK with iOS 15. But we are told to use connectedScenes instead… So I adapt code: var keyBoardWindow: UIWindow? = nil if #available(iOS 13.0, *) { // use connectedScenes let scenes = UIApplication.shared.connectedScenes let windowScene = scenes.first as? UIWindowScene if let kbWindow = windowScene?.windows.last { keyBoardWindow = kbWindow } } else { keyBoardWindow = UIApplication.shared.windows.last } It runs, but subview does not show. The problem is that keyboardWindow used to be a UIRemoteKeyboardWindow (the true keyboard window) and now is UITextEffectsWindow, which is not the real keyboardWindow. So adding subview to it adds improperly in the hierarchy. Note: someone detailed the view hierarchy here: https://developer.apple.com/forums/thread/664547 UIWindow UITextEffectsWindow     UIInputWindowController         UIInputSetContainerView               UIInputSetHostView               UIEditingOverlayViewController                      UIEditingOverlayGestureView I guess I have to add subview to something else than UITextEffectsWindow (keyBoardWindow), but to what ? I tried      keyBoardWindow = kbWindow.superview as? UIWindow to no avail. I also tried to debug view hierarchy, but keyboard does not show in debugView.
9
0
2.9k
Jan ’23
Handwritten text recognition working poorly on iOS 16 beta ?
That's a follow up of a previous thread. https://developer.apple.com/forums/thread/707130 I did some test on iOS 16 simulator with Xcode 14ß. Recognition is very poor. And recognition rate of some single letters (an L or an I for instance) is zero (literally). Same code worked better (success 50% for same single letters) with iOS 15.2. Did something change on iOS 16 ? I filed a bug report: Jun 7, 2022 at 3:28 PM – FB10066541
3
0
2.4k
Jan ’23
Failed to install WatchOS 9.1 simulator runtime in Xcode 14.2
I installed Xcode 14.2 (in parallel with other versions of Xcode with different names) on MBP MacOS 12.6.2. Il works OK except when trying to use WatchOS simulator. When I select a WatchOS target and then look for simulator, none is installed. I get an item in menu proposing to GET 9.1. I downloaded. But at the end of download, installation failed with the message that installation of watchOS 9.1 simulator runtime failed in CoreSimulator. I tried the solution proposed here, to no avail. https://stackoverflow.com/questions/74096242/unable-to-select-device-for-watchos-app-in-xcode Note: installation on an iMac running 12.6.2 and Xcode 14.2 shows a long list of simulators:
1
0
1.7k
Dec ’22
Button behaviour changes if position(x:y)
With this code, button is at the center of the view and message is logged only when tapping in the button 1. struct ContentView: View { 2. var body: some View { 3. 4. ZStack { 5. Button(action: { 6. print("Touched") 7. }) { 8. Image(systemName: "square.split.diagonal.2x2") 9. .font(.system(size: 24)) 10. .foregroundColor(.black) 11. // .position(x: 12, y: 12) 12. } 13. 14. } 15. .frame(width: 300, height: 300) 16. .background(Color.yellow) 17. } 18. } But if I uncomment line 11, the message is logged on tap anywhere in the view. How is it position() changes the behaviour ?
3
0
1.1k
Dec ’22
ViewBuilder does not force View update
I use a ViewBuilder to generate the destination used in a NavigationSplitView (to select the detail View). This viewBuilder depends on a parameter (in fact 2, but I try to simplify). ViewBuilder is simple, just calls a View: @ViewBuilder func destination(object: SomeObject, name: String) -> some View { MyView(objToUse: object, nameToUse: name) } But this does not work. When I change the selection in the Master of the splitView, view is not updated (even though I've checked the content is updated. This si so simple that I started using directly MyView(objToUse: object, nameToUse: name) in the detail View. It did not work either. Now, here is the surprise: if I use a switch statement in the ViewBuilder, it works: Let's say we have: struct SomeContent: Hashable, Identifiable, Codable { var id = UUID() var name: String } struct Object : Hashable, Identifiable, Equatable, Codable { var id = UUID() var content: [SomeContent] = [] } So I define a func to get all the names func allNames(of object: SomeObject) -> [String] { var names : [String] = [] for o in object.content { names.append(o.name) } return names } And modify ViewBuilder as follows: it works @ViewBuilder func destination(object: SomeObject, name: String) -> some View { let names : [String] = allNames(of: object) switch name { case names[0]: MyView(objToUse: object, nameToUse: name) case names[1]: MyView(objToUse: object, nameToUse: name) case names[2]: MyView(objToUse: object, nameToUse: name) default: EmptyView() } It also works with nested if else if instead of a switch. What is it I am missing ?
2
0
912
Jan ’23
Design pattern for scheduling simulation steps
I'm running a simulation (SwiftUI app), which has 100 steps. I need each step to be executed in order. A first try was to dispatch with delay to schedule each second: for step in 0..<100 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(step) * 1.0) { // simulation code } } Very poor results as 100 running threads are too much load for the system. So I split in 2 stages: for bigStep in 0..<10 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(bigStep) * 10.0 ) { for step in 0..<10 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(step) * 1.0) { // simulation code } } } } It works much better, as now there are a max of 20 threads active (in fact I create more levels to limit to a max of 8 concurrent threads). It addition, it allows to interrupt the simulation before end. My questions: is it the appropriate pattern ? Would a timer be better ? Other options ?
4
0
1.2k
May ’23
Dark mode switching modifies ObservedObject
This is a SwiftUI Mac App. There is an observable object with a published array class AllObjects : ObservableObject { @Published var theItems = [Item]() } In the View, observed with: @ObservedObject var allObjects : AllObjects Item class is : class Item: ObservableObject, Identifiable { let id = UUID() var placeNo = 1 // Other properties } When I switch dark / light mode within the app (with a toggle) @Environment(\.colorScheme) var colorScheme the ObservedObject allObjects is modified: array is emptied: allObjects.theItems is now an empty array. What could cause the change to theItems ? I have checked that the only func un which theItems is reset to [] is not called.
2
0
685
Apr ’23