-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import SwiftUI | ||
|
||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *) | ||
public extension View { | ||
func readSize( | ||
onChange: @escaping (CGSize) -> Void | ||
) -> some View { | ||
readFrame(in: .local) { onChange($0.size) } | ||
} | ||
|
||
func readFrame( | ||
in coordinateSpace: CoordinateSpace, | ||
onChange: @escaping (CGRect) -> Void | ||
) -> some View { | ||
background( | ||
GeometryReader { geometryProxy in | ||
Color.clear | ||
.preference(key: FramePreferenceKey.self, value: geometryProxy.frame(in: coordinateSpace)) | ||
} | ||
) | ||
.onPreferenceChange(FramePreferenceKey.self, perform: onChange) | ||
} | ||
} | ||
|
||
private struct FramePreferenceKey: PreferenceKey { | ||
static var defaultValue: CGRect = .zero | ||
static func reduce(value: inout CGRect, nextValue: () -> CGRect) { } | ||
} | ||
|
||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *) | ||
#Preview { | ||
Text("Hello, World!") | ||
.readSize { | ||
print("Size:", $0) | ||
} | ||
.readFrame(in: .local) { | ||
print("Local frame:", $0) | ||
} | ||
.readFrame(in: .global) { | ||
print("Global frame:", $0) | ||
} | ||
} |