Posts

Post not yet marked as solved
5 Replies
28k Views
Say you have the following view:import SwiftUI struct BindingViewExample_1 : View { @State var value = false private var text: String { "Toggle is " + (value ? "'on'" : "'off'") } var body: some View { HStack { Toggle(isOn: $value) { Text(text) } } .padding([.leading, .trailing], 80) } } #if DEBUG struct BindingViewExample_1_Previews : PreviewProvider { static var previews: some View { BindingViewExample_1(value: true) } } #endifThis previews fine and you can interact with the control in the preview and see the changes immediately.But what if it took a *binding* as input? Consider this alternate version of the same view:import SwiftUI struct BindingViewExample_2 : View { @Binding var value: Bool private var text: String { "Toggle is " + (value ? "'on'" : "'off'") } var body: some View { HStack { Toggle(isOn: $value) { Text(text) } } .padding([.leading, .trailing], 80) } } #if DEBUG struct BindingViewExample_2_Previews : PreviewProvider { @State static var value = false static var previews: some View { BindingViewExample_2(value: $value) } } #endifThe only differences are in line 5 for the View itself, and in the preview code for both views. This compiles without errors but the preview no longer works. XCode just refuses to even create a preview.If you keep the View as is but change the preview code to this, instead,#if DEBUG struct BindingViewExample_2_Previews : PreviewProvider { static var previews: some View { BindingViewExample_2(value: .constant(true)) } } #endifXcode now builds and displays a preview but interacting with it doesn't update the view. That's not surprising, given the `.constant(true)` binding, but that's the only way I managed to make the preview display when the View takes a binding and that's not very useful (since I can't interact with it).So... what's the right way to write preview code for a View that takes bindings as inputs?
Posted
by oro_boris.
Last updated
.
Post marked as solved
4 Replies
1.1k Views
Hello, I'd like to perform some math on a fairly large number of CGPoint instances stored in an array so I thought I'd look into using Accelerate and start by conforming CGPoint to AccelerateBuffer, followed by extending Array to conform as well. My problem is that I don't understand what to do with the requirement public func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<CGFloat>) throws -> R) rethrows -> R How am I supposed to implement this? Are there any code examples somewhere that I can look into? Many thanks.
Posted
by oro_boris.
Last updated
.
Post not yet marked as solved
0 Replies
428 Views
I was playing with drawing shapes and got stuck trying to fill and stroke them with different colors.Both lines 6 and 7 work fine in isolation but, together, they give a compiler error. I understand *why* there's an error - it's because applying either of those lines in isolation changes the type from `Circle` to some other type (`ShapeView<StrokedShape<Circle>, Color>` or `ShapeView<Circle, Color>`) and that other type doesn't support stroking or filling - but I don't think the API should be so counter-intuitive.More importantly, what *is* the correct way to apply both a stroke color and a fill color to a shape?import SwiftUI struct CircleView : View { var body: some View { Circle() .stroke(Color.green, lineWidth: 5) //.fill(Color.orange) } } #if DEBUG struct CircleView_Previews : PreviewProvider { static var previews: some View { VStack { CircleView() .background(Color.primary) .colorScheme(.light) CircleView() .background(Color.primary) .colorScheme(.dark) } } } #endif
Posted
by oro_boris.
Last updated
.
Post not yet marked as solved
3 Replies
1.2k Views
First, a little context. I was trying to create a simple animation of a view that pulsates indefinitely and found it to be a really frustrating experience, probably because I don’t yet understand the SwiftUI animation model correctly. If someone has a simple solution, please share.Anyway, it occurred to me that it would be nice to have a view modifier that you attach to a view to express the desire to “animate property X of the view from value ‘from’ to value ‘to’, starting at value ‘startingAt’, using animation ‘animation’. All the view needs to do is expose that property X, as a key path or maybe a binding, and SwiftUI does the rest.This would allow for an easy way to animate several properties independently but simultaneously, each with their own animation parameters, as well as a way to animate a group of properties together (the view exposes some property which it uses internally to control a group of properties).Is this something that’s already possible and easy to do? If so, how?Here’s what the setup above would/might look like in actual code:struct ContentView: View { // Note that, in this setup, x, y, and z are all animated independently from // each other, and with their own custom parameters. // Properties can be animated together by exposing a single property that // controls them internally (as 'z' does in the example below). // All parameters are optional: // - 'from' defaults to 0 // - 'to' defaults to 1 // - 'startingAt' defaults to 'from' // - the animation defaults to 'basic()' with its default parameters // Instead of exposing a key-path, perhaps exposing a binding would make // more sense and/or be more appropriate. var body: some View { SomeView() // .animating(\.x, from: 0.3, to: 0.8, startingAt: 0.4, // Animation.basic().repeatForever(autoreverses: true)) // .animating(\.y, from: 5.0, to: 3.0, startingAt: 3.5, // Some Animation here) // .animating(\.z, from: 0.25, to: 0.7, startingAt: 0.5, // Some Animation here) }}struct SomeView: View { // Some property exposed by SomeView, which some // aspect of its layout or rendering depends on. var x: CGFloat = 0 // Some other property exposed by SomeView, which some // other aspect of its layout or rendering depends on. var y: Double = 0 // This property is used internally to control several other // such properties, in essence animating them together. var z: Double = 0 var body: some View { Circle() .fill(Color.red) }}
Posted
by oro_boris.
Last updated
.