-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
1478498
commit 94b9c88
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
Projects/Shared/DesignSystem/Sources/Extension/FrameMeasure.swift
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,52 @@ | ||
// | ||
// FrameMeasure.swift | ||
// DesignSystem | ||
// | ||
// Created by devMinseok on 8/15/24. | ||
// Copyright © 2024 PomoNyang. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public struct FrameMeasurePreferenceKey: PreferenceKey { | ||
public typealias Value = [AnyHashable: CGRect] | ||
|
||
public static var defaultValue: Value = Value() | ||
|
||
public static func reduce(value: inout Value, nextValue: () -> Value) { | ||
value.merge(nextValue()) { _, new in | ||
new | ||
} | ||
} | ||
} | ||
|
||
private struct FrameMeasureGeometry: View { | ||
let space: CoordinateSpace | ||
let identifier: AnyHashable | ||
|
||
var body: some View { | ||
GeometryReader { geometry in | ||
Color.clear | ||
.preference( | ||
key: FrameMeasurePreferenceKey.self, | ||
value: [identifier: geometry.frame(in: space)] | ||
) | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
/// Frame 측정을 위한 메서드 | ||
public func setFrameMeasure(space: CoordinateSpace, identifier: AnyHashable) -> some View { | ||
return self.background( | ||
FrameMeasureGeometry(space: space, identifier: identifier) | ||
) | ||
} | ||
|
||
/// 측정된 Frame을 가져오기 위한 메서드 | ||
public func getFrameMeasure(perform action: @escaping (FrameMeasurePreferenceKey.Value) -> Void) -> some View { | ||
return self.onPreferenceChange(FrameMeasurePreferenceKey.self) { value in | ||
action(value) | ||
} | ||
} | ||
} |