Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Created April 11, 2024 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewolbrich/8168539a92c0a5448b9fe053b022bd74 to your computer and use it in GitHub Desktop.
Save drewolbrich/8168539a92c0a5448b9fe053b022bd74 to your computer and use it in GitHub Desktop.
//
// View+AttachmentPivot.swift
//
// Created by Drew Olbrich on 4/11/24.
// Copyright © 2024 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import SwiftUI
extension View {
/// Sets the pivot position when this view is used as a `ViewAttachmentEntity` in a
/// `RealityView`.
///
/// For example, if `.bottom` is specified, then the view attachment entity's
/// `position` property will specify the position in space corresponding to the
/// bottom center of the view.
///
/// In the absence of this view modifier, the default behavior for `RealityView`
/// attachments is the equivalent of `attachmentPivot(.center)`.
///
/// ## Example Usage:
///
/// ```
/// RealityView { content, attachments in
/// ...
/// } attachments: {
/// Attachment(id: "my-attachment-id") {
/// MyAttachment()
/// .attachmentPivot(.bottom)
/// }
/// }
/// ```
func attachmentPivot(_ pivotAlignment: Alignment) -> some View {
return modifier(AttachmentPivotViewModifier(pivotAlignment: pivotAlignment))
}
}
private struct AttachmentPivotViewModifier: ViewModifier {
let pivotAlignment: Alignment
@Environment(\.layoutDirection) private var layoutDirection
func body(content: Content) -> some View {
GeometryReader { geometryProxy in
let offset = offset(for: pivotAlignment, geometryProxy: geometryProxy)
HStack {
if pivotAlignment.horizontal != .leading {
Spacer(minLength: 0)
}
VStack {
if pivotAlignment.vertical != .top {
Spacer(minLength: 0)
}
content
if pivotAlignment.vertical != .bottom {
Spacer(minLength: 0)
}
}
if pivotAlignment.horizontal != .trailing {
Spacer(minLength: 0)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.offset(x: offset.x, y: offset.y)
}
}
private func offset(for alignment: Alignment, geometryProxy: GeometryProxy) -> CGPoint {
let x: CGFloat
let y: CGFloat
switch alignment.horizontal {
case .leading:
switch layoutDirection {
case .leftToRight:
x = geometryProxy.size.width/2
case .rightToLeft:
x = -geometryProxy.size.width/2
@unknown default:
assertionFailure("Unsupported layout direction: \(layoutDirection)")
x = 0
}
case .trailing:
switch layoutDirection {
case .leftToRight:
x = -geometryProxy.size.width/2
case .rightToLeft:
x = geometryProxy.size.width/2
@unknown default:
assertionFailure("Unsupported layout direction: \(layoutDirection)")
x = 0
}
case .center:
x = 0
default:
assertionFailure("Unsupported horizontal alignment: \(alignment.horizontal)")
x = 0
}
switch alignment.vertical {
case .top:
y = geometryProxy.size.height/2
case .bottom:
y = -geometryProxy.size.height/2
case .center:
y = 0
default:
assertionFailure("Unsupported vertical alignment: \(alignment.horizontal)")
y = 0
}
return CGPoint(x: x, y: y)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment