How to accomplish this?
By using geometry reader to get the parent width instead of using @State variables.
struct ContentView: View {
var body: some View {
MyView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.border(Color.green, width: 1)
}
}
struct MyView: View {
var body: some View {
GeometryReader { proxy in
Button("Update Width") {}
.frame(width: proxy.size.width * 0.4, height: proxy.size.height)
.background(Color.blue)
}
}
}
Post
Replies
Boosts
Views
Activity
Thanks for your reply. It is some kind of bug in macOS I think. Because when I try the same code on a different account, it works perfectly. But on my own user account it shows the bug.
I still have not figured out how to get the menu items to render correctly on my account though... 😞
I think you might be mixing two different states in an incorrect manner. OK, you need @State for MyView's position that you want to drag around. But the size of it's parent should not be included as a @State variable in this View. It is someone else's state and you should not duplicate one state multiple times.
You can read the size of the parent using a GeometryReader or you can pass its size using constants. Code example of both possibilities in combination with draggable position are listed below
Reading parent size using GeometryReader
struct ContentView: View {
var body: some View {
MyView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.border(Color.green, width: 1)
}
}
struct MyView: View {
@State var relativePosition = CGPoint(x: 0.20, y: 0.2)
@State var relativeSize = CGSize(width: 0.1, height: 0.1)
var body: some View {
GeometryReader(content: rectangle(parent:))
}
func rectangle(parent: GeometryProxy) -> some View {
let absoluteX = relativePosition.x * parent.size.width
let absoluteY = relativePosition.y * parent.size.height
return Rectangle()
.frame(width: parent.size.width * relativeSize.width, height: parent.size.height * relativeSize.height)
.position(x: absoluteX, y: absoluteY)
.gesture(dragGesture(parentSize: parent.size))
}
func dragGesture(parentSize: CGSize) -> some Gesture {
DragGesture().onChanged { (drag) in
let relativeX = drag.location.x / parentSize.width
let relativeY = drag.location.y / parentSize.height
self.relativePosition = CGPoint(x: relativeX, y: relativeY)
}
}
}
Passing the size using constants
struct ContentView: View {
var body: some View {
GeometryReader { proxy in
MyView(parentSize: proxy.size)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.border(Color.green, width: 1)
}
}
}
struct MyView: View {
let parentSize: CGSize
@State var relativePosition = CGPoint(x: 0.20, y: 0.2)
@State var relativeSize = CGSize(width: 0.1, height: 0.1)
var body: some View {
Rectangle()
.frame(width: parentSize.width * relativeSize.width, height: parentSize.height * relativeSize.height)
.position(absolutePosition)
.gesture(dragGesture)
}
var dragGesture: some Gesture {
DragGesture().onChanged { (drag) in
let relativeX = drag.location.x / self.parentSize.width
let relativeY = drag.location.y / self.parentSize.height
self.relativePosition = CGPoint(x: relativeX, y: relativeY)
}
}
var absolutePosition: CGPoint {
CGPoint(
x: relativePosition.x * parentSize.width,
y: relativePosition.y * parentSize.height
)
}
}
Hi thanks for the response.
connection.currentPath.remoteEndpoint is the broadcast address in my example.
I didn't use an NWListener, I called receiveMessage on the NWConnection from which I am sending the beacons. I did this because I need to listen to the port from which the beacons are sent.
How do I create a NWListener that binds to the same port my NWConnection uses to send the beacon?
Were you able to resolve this issue?
Thanks for the quick response Matt. - https://developer.apple.com/forums/profile/meaton I looked at your other post - https://developer.apple.com/forums/thread/132575 but that is demonstrating a TLV (type, length, value) framer. I was talking about separating frames using a separation character (or delimiter).
you will need to save previously read data so that it can fit into the next frame Hmm... Why should we be saving the old chunk's data while it is still available in NWProtocolFramer.Instance's parseInput(...) function. The old data (which you refer to as previously read data) is not lost (and I don't think it is marked read until you call deliverInput...(...) or return its length in parseInput(...)). The problem I am experiencing, is that the framer does not get the new data (but is stuck on the old data).
I made small GitHub repo where you can debug the problem: https://github.com/Dev1an/Simple-Framer
Could you explain how your other posts (TicTacToe & TLV) are relevant to this delimiting problem (where the message lengths are not known in advance)?
Do you have any new insights on this problem? I'm seeing the same behaviour with URLSession tasks that keep accumulating in instruments.