Skip to content

Commit

Permalink
feature: FrameMeasure 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
devMinseok committed Aug 15, 2024
1 parent 1478498 commit 94b9c88
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Projects/Shared/DesignSystem/Sources/Extension/FrameMeasure.swift
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)
}
}
}

0 comments on commit 94b9c88

Please sign in to comment.