-
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.
Showing
3 changed files
with
77 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// RegionsListVC+CustomRotor.swift | ||
// GeofenceTester | ||
// | ||
// Created by Alexander von Below on 08.07.22. | ||
// | ||
|
||
import UIKit | ||
import MapKit | ||
|
||
extension RegionsListViewController { | ||
func setupCustomRotor () { | ||
// https://stackoverflow.com/questions/42170870/create-a-custom-voiceover-rotor-to-navigate-mkannotationviews | ||
|
||
let markerRotor = UIAccessibilityCustomRotor(name: NSLocalizedString("Markers", | ||
comment: "Rotor Title")) | ||
{ predicate in | ||
let forward = (predicate.searchDirection == .next) | ||
|
||
// which element is currently highlighted | ||
if (predicate.currentItem.targetElement == nil) { | ||
self.logger.info("Current Item Target Element is nil") | ||
} | ||
let currentAnnotationView = predicate.currentItem.targetElement as? MKAnnotationView | ||
if (currentAnnotationView == nil) { | ||
self.logger.error("Target Element is \(predicate.currentItem.targetElement.debugDescription)") | ||
} | ||
let currentAnnotation = (currentAnnotationView?.annotation as? MKAnnotation) | ||
|
||
// easy reference to all possible annotations | ||
let allAnnotations = self.mapView.annotations | ||
|
||
// we'll start our index either 1 less or 1 more, so we enter at either 0 or last element | ||
var currentIndex = forward ? -1 : allAnnotations.count | ||
|
||
// set our index to currentAnnotation's index if we can find it in allAnnotations | ||
if let currentAnnotation = currentAnnotation { | ||
if let index = allAnnotations.firstIndex(where: { (annotation) -> Bool in | ||
return (annotation.coordinate.latitude == currentAnnotation.coordinate.latitude) && | ||
(annotation.coordinate.longitude == currentAnnotation.coordinate.longitude) | ||
}) { | ||
currentIndex = index | ||
} | ||
} | ||
|
||
// now that we have our currentIndex, here's a helper to give us the next element | ||
// the user is requesting | ||
let nextIndex = {(index:Int) -> Int in forward ? index + 1 : index - 1} | ||
|
||
currentIndex = nextIndex(currentIndex) | ||
|
||
while currentIndex >= 0 && currentIndex < allAnnotations.count { | ||
let requestedAnnotation = allAnnotations[currentIndex] | ||
|
||
// i can't stress how important it is to have animated set to false. save yourself the 10 hours i burnt, and just go with it. if you set it to true, the map starts moving to the annotation, but there's no guarantee the annotation has an associated view yet, because it could still be animating. in which case the line below this one will be nil, and you'll have a whole bunch of annotations that can't be navigated to | ||
self.mapView.setCenter(requestedAnnotation.coordinate, animated: false) | ||
if let annotationView = self.mapView.view(for: requestedAnnotation) { | ||
let title: String = (requestedAnnotation.title ?? "Unknown") ?? "Unknown" | ||
self.logger.info("We want to be returning \(title) Index \(currentIndex)") | ||
return UIAccessibilityCustomRotorItemResult(targetElement: annotationView, targetRange: nil) | ||
} | ||
|
||
currentIndex = nextIndex(currentIndex) | ||
} | ||
self.logger.info("We have nothing") | ||
return nil | ||
} | ||
self.mapView.accessibilityCustomRotors = [markerRotor] | ||
} | ||
} |
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