Created
May 20, 2020 14:06
-
-
Save MrLotU/e3d843a1166314276295f095eae6c826 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
func > (_ lhs: CGSize, _ rhs: CGSize) -> Bool { | |
return lhs.width > rhs.width && lhs.height > rhs.height | |
} | |
struct MyView: View { | |
@State var biggestSize: CGSize = .zero | |
var h: CGFloat? { | |
self.biggestSize == .zero ? nil : biggestSize.height | |
} | |
var body: some View { | |
GeometryReader { geo in | |
VStack { | |
Text("ABC") | |
.background(Color.green) | |
.transformAnchorPreference(key: ViewFrameKey.self, value: .bounds) { (array, anchor) in | |
// Array is of type `ViewFrameKey.Value`. anchor is an instance of `Anchor<CGRect>` | |
// Here we grab the frame data for this view (specified by the anchor) | |
// from the GeometryProxy and add store it in our `ViewFrameKey` store. | |
array.append(.init(id: "firstRectangle", frame: geo[anchor])) | |
} | |
.onPreferenceChange(ViewFrameKey.self) { (frames) in | |
// frames is of type `ViewFrameKey.Value` | |
// Here we have a list of frames where `frames.first` is the last updated one. | |
guard let frame = frames.first else { return } | |
let size = frame.frame.size | |
if size > self.biggestSize { | |
self.biggestSize = size | |
} | |
} | |
.frame(height: self.h) | |
Rectangle() | |
.foregroundColor(.red) | |
.transformAnchorPreference(key: ViewFrameKey.self, value: .bounds) { (array, anchor) in | |
// Array is of type `ViewFrameKey.Value`. anchor is an instance of `Anchor<CGRect>` | |
// Here we grab the frame data for this view (specified by the anchor) | |
// from the GeometryProxy and add store it in our `ViewFrameKey` store. | |
array.append(.init(id: "secondRectangle", frame: geo[anchor])) | |
} | |
.onPreferenceChange(ViewFrameKey.self) { (frames) in | |
// frames is of type `ViewFrameKey.Value` | |
// Here we have a list of frames where `frames.first` is the last updated one. | |
guard let frame = frames.first else { return } | |
let size = frame.frame.size | |
if size > self.biggestSize { | |
self.biggestSize = size | |
} | |
} | |
.frame(height: self.h) | |
} | |
} | |
} | |
} | |
struct ViewFrame: Equatable { | |
let id: String | |
let frame: CGRect | |
static func == (lhs: ViewFrame, rhs: ViewFrame) -> Bool { | |
lhs.id == rhs.id && lhs.frame == rhs.frame | |
} | |
} | |
struct ViewFrameKey: PreferenceKey { | |
typealias Value = [ViewFrame] | |
static var defaultValue: [ViewFrame] = [] | |
static func reduce(value: inout [ViewFrame], nextValue: () -> [ViewFrame]) { | |
value.append(contentsOf: nextValue()) | |
} | |
} | |
struct MyView_Previews: PreviewProvider { | |
static var previews: some View { | |
MyView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment