Skip to content

Commit

Permalink
Make anchorPoint a UnitPoint instead of CGPoint
Browse files Browse the repository at this point in the history
`UnitPoint` - https://developer.apple.com/documentation/swiftui/unitpoint - is more SwiftUI-y than `CGPoint`, and will allow the anchorPoint to use the constants like `.center` and `.bottom`.

Extract extensions to make it trivial to convert the `UnitPoint` to a `centerOffset` within a rect, and to convert back to a `CGPoint` for the `UIView` `anchorPoint` property.
  • Loading branch information
darronschall committed Oct 2, 2023
1 parent 8d08f92 commit f9dce1b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
20 changes: 4 additions & 16 deletions Sources/Annotations/MKMapAnnotationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,13 @@ class MKMapAnnotationView<Content: View>: MKAnnotationView {
controller.view.backgroundColor = .clear
#endif

if #available(iOS 16.0, *) {
anchorPoint = mapAnnotation.anchorPoint
if #available(iOS 16, *) {
anchorPoint = mapAnnotation.anchorPoint.toCGPoint()
} else {
centerOffset = anchorPointToCenterOffset(mapAnnotation.anchorPoint, in: bounds)
centerOffset = mapAnnotation.anchorPoint.toCenterOffset(in: bounds)
}
}

// Only necessary for iOS < 16, where `anchorPoint` is not available on `UIView`
func anchorPointToCenterOffset(_ anchorPoint: CGPoint, in rect: CGRect) -> CGPoint {
assert((0.0...1.0).contains(anchorPoint.x), "Valid anchor point range is 0.0 to 1.0, received x value: \(anchorPoint.x)")
assert((0.0...1.0).contains(anchorPoint.y), "Valid anchor point range is 0.0 to 1.0, received y value: \(anchorPoint.y)")

return .init(
x: (0.5 - anchorPoint.x) * rect.width,
y: (0.5 - anchorPoint.y) * rect.height
)
}


// MARK: Overrides

#if canImport(UIKit)
Expand All @@ -77,7 +65,7 @@ class MKMapAnnotationView<Content: View>: MKAnnotationView {
controller.view.frame = bounds

if #unavailable(iOS 16.0), let mapAnnotation {
centerOffset = anchorPointToCenterOffset(mapAnnotation.anchorPoint, in: bounds)
centerOffset = mapAnnotation.anchorPoint.toCenterOffset(in: bounds)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Annotations/ViewMapAnnotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct ViewMapAnnotation<Content: View>: MapAnnotation {
// MARK: Stored Properties

public let annotation: MKAnnotation
let anchorPoint: CGPoint
let anchorPoint: UnitPoint
let clusteringIdentifier: String?
let displayPriority: MKFeatureDisplayPriority
let collisionMode: MKAnnotationView.CollisionMode
Expand All @@ -53,7 +53,7 @@ public struct ViewMapAnnotation<Content: View>: MapAnnotation {
coordinate: CLLocationCoordinate2D,
title: String? = nil,
subtitle: String? = nil,
anchorPoint: CGPoint = .init(x: 0.5, y: 0.5),
anchorPoint: UnitPoint = .center,
clusteringIdentifier: String? = nil,
displayPriority: MKFeatureDisplayPriority = .required,
collisionMode: MKAnnotationView.CollisionMode = .rectangle,
Expand All @@ -69,7 +69,7 @@ public struct ViewMapAnnotation<Content: View>: MapAnnotation {

public init(
annotation: MKAnnotation,
anchorPoint: CGPoint = .init(x: 0.5, y: 0.5),
anchorPoint: UnitPoint = .center,
clusteringIdentifier: String? = nil,
displayPriority: MKFeatureDisplayPriority = .required,
collisionMode: MKAnnotationView.CollisionMode = .rectangle,
Expand Down
25 changes: 25 additions & 0 deletions Sources/Extensions/UnitPoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// File.swift
//
//
// Created by Darron Schall on 10/2/23.
//

import SwiftUI

extension UnitPoint {

func toCGPoint() -> CGPoint {
return CGPoint(x: x, y: y)
}

func toCenterOffset(in rect: CGRect) -> CGPoint {
return CGPoint(
x: (0.5 - x) * rect.width,
y: (0.5 - y) * rect.height
)
}

}


0 comments on commit f9dce1b

Please sign in to comment.