Skip to content

Commit

Permalink
Add support for custom tappable area in Interaction (#2364)
Browse files Browse the repository at this point in the history
  • Loading branch information
persidskiy authored Nov 25, 2024
1 parent d061b47 commit e4f30f2
Show file tree
Hide file tree
Showing 24 changed files with 482 additions and 56 deletions.
4 changes: 4 additions & 0 deletions Apps/Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
A6389C28B8AAC39878591AD0 /* PitchAndDistanceExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A5C0A3C44715B96D646ACB7 /* PitchAndDistanceExample.swift */; platformFilters = (ios, ); };
A6A68B4ED674A924ACBD8FA2 /* UIColor+Random.swift in Sources */ = {isa = PBXBuildFile; fileRef = F000C4D3B6FC70FA9607E3A3 /* UIColor+Random.swift */; platformFilters = (ios, ); };
A972D3306BC53DEC9798C60D /* ExternalVectorSourceExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 133E4EABC7540ED460F08B8F /* ExternalVectorSourceExample.swift */; platformFilters = (ios, ); };
AD0922FA7F69AEE4C23F2351 /* InteractionsPlayground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 388932B3A65BB7E9B59FDBE0 /* InteractionsPlayground.swift */; };
AE51E276DCD8CF89AB339224 /* LongTapAnimationExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB76F486D80FED88678B04D /* LongTapAnimationExample.swift */; platformFilters = (ios, ); };
AE6E90DB7B6DA4580C2DAB59 /* FrameViewAnnotationsExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC0F09FF1EF1A88BC1C6545 /* FrameViewAnnotationsExample.swift */; platformFilters = (ios, ); };
B304BACFCD08802A740E8919 /* PuckPlayground.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0CC67084BA1191D0B179A94 /* PuckPlayground.swift */; };
Expand Down Expand Up @@ -204,6 +205,7 @@
3595A6E8FB1FD9F41DEB5C6F /* AnnotationsOrderTestExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnnotationsOrderTestExample.swift; sourceTree = "<group>"; };
370DFCA52EB6C7F119BF81DA /* MapEventsExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapEventsExample.swift; sourceTree = "<group>"; };
384FD8FC97B9F5011AF4BD61 /* GlobeFlyToExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobeFlyToExample.swift; sourceTree = "<group>"; };
388932B3A65BB7E9B59FDBE0 /* InteractionsPlayground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InteractionsPlayground.swift; sourceTree = "<group>"; };
3BCB1CC4577300FEF4DE017B /* InsetMapExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InsetMapExample.swift; sourceTree = "<group>"; };
3BDE7738CA55957F3FAC3ECE /* LineAnnotationExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LineAnnotationExample.swift; sourceTree = "<group>"; };
3E2F68B22AFF73A71F86CABC /* ExamplesUITests.xctest */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.cfbundle; path = ExamplesUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -341,6 +343,7 @@
children = (
3595A6E8FB1FD9F41DEB5C6F /* AnnotationsOrderTestExample.swift */,
7A77AEDBF679F223D4412FEE /* AttributionDialogueExamples.swift */,
388932B3A65BB7E9B59FDBE0 /* InteractionsPlayground.swift */,
65535FB9F190778001AB847A /* MapScrollExample.swift */,
E6E875420B5C674C8CCAB9B1 /* MapSettingsExample.swift */,
C0CC67084BA1191D0B179A94 /* PuckPlayground.swift */,
Expand Down Expand Up @@ -877,6 +880,7 @@
B9B1EE72E6203358F2785916 /* IconSizeChangeExample.swift in Sources */,
423A42B555DD0B3AD4856FCF /* InsetMapExample.swift in Sources */,
94DB7E8C829041DC5F5B2300 /* InstrumentClusterCarPlaySceneDelegate.swift in Sources */,
AD0922FA7F69AEE4C23F2351 /* InteractionsPlayground.swift in Sources */,
8B4085733CCABE3BE3D16F7E /* LayerPositionExample.swift in Sources */,
918F4BDCC25819DD68BC9518 /* LayerSlotExample.swift in Sources */,
C04160BF66055F7DE9315395 /* Lights3DExample.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Apps/Examples/Examples/SwiftUI Examples/SwiftUIRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct SwiftUIRoot: View {

Section {
ExampleLink("Map settings", note: "Showcase of the most possible map configurations.", destination: MapSettingsExample())
ExampleLink("Interactions playground", note: "Interactions edge cases", destination: InteractionsPlayground())
ExampleLink("Viewport Playground", note: "Showcase of the possible viewport states.", destination: ViewportPlayground())
ExampleLink("Puck playground", note: "Display user location using puck.", destination: PuckPlayground())
ExampleLink("Annotation Order", destination: AnnotationsOrderTestExample())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
@_spi(Experimental) import MapboxMaps
import SwiftUI

@available(iOS 14.0, *)
struct InteractionsPlayground: View {
@State private var text: String?
@State private var tap: Tap?
@State private var mapTap: Tap?
@State private var disableTerrain: Bool = false

@State private var routes = [
Route(line: LineString(route1), isActive: true),
Route(line: LineString(route2), isActive: false)
]

var body: some View {
ZStack {
let cameraCenter = CLLocationCoordinate2D(latitude: 60.1718, longitude: 24.9453)
Map(initialViewport: .camera(center: cameraCenter, zoom: 16.35, bearing: 49.92, pitch: 40)) {

let polygon = Polygon(center: .helsinki, radius: 200, vertices: 30)
PolygonAnnotationGroup {
PolygonAnnotation(polygon: polygon)
.fillColor(.green)
.fillOpacity(0.2)
.onTapGesture { _ in
text = "Polygon tap"
return true
}
}
.layerId("polygon")
.slot("bottom")

PolylineAnnotationGroup(routes) { route in
PolylineAnnotation(lineString: route.line)
.lineColor(route.isActive ? "#57A9FB" : "gray")
.lineBorderColor(route.isActive ? "#327AC2" : "black")
.lineSortKey(route.isActive ? 1 : 0)
.onTapGesture { ctx in
text = "Tap route"
tap = Tap(pos: ctx.point, radius: 22)

let id = route.id
routes = routes.map { route in
var r = route
r.isActive = route.id == id
return r
}
return true
}
}
.tapRadius(22)
.lineWidth(10)
.lineBorderWidth(2)
.lineCap(.round)
.slot("middle")

TapInteraction(.standardPoi, radius: 0) { feature, ctx in
text = "Tap poi \(feature.name ?? "-"), r: 0"
tap = Tap(pos: ctx.point)
return true
}

TapInteraction(.standardPoi, radius: 8) { feature, ctx in
text = "Tap poi \(feature.name ?? "-"), r: 8"
tap = Tap(pos: ctx.point, radius: 8)
return true
}

TapInteraction(.standardPlaceLabels, radius: 10) { feature, ctx in
text = "Tap place \(feature.name ?? "-"), r: 10"
tap = Tap(pos: ctx.point, radius: 10)
return true
}

if disableTerrain {
Terrain(sourceId: "fake")
}
TapInteraction { ctx in
mapTap = Tap(pos: ctx.point)
text = nil
tap = nil
return true
}
}
.debugOptions(.collision)
.mapStyle(.standardExperimental)

GeometryReader { _ in
if let mapTap {
TapView(tap: mapTap, color: .blue)
.id(mapTap.id)
}
if let tap {
TapView(tap: tap, color: .red)
.id(tap.id)
}
}
}
.ignoresSafeArea()
.safeOverlay(alignment: .bottom) {
VStack {
if let text {
Text(text)
.floating()
}
HStack {
Text("Disable Terrain")
Toggle("dis", isOn: $disableTerrain)
}.floating()
}
}

}
}

private struct Route: Identifiable {
var id = UUID()
var line: LineString
var isActive: Bool
}

private let route1 = [
CLLocationCoordinate2D(latitude: 60.17047709327494, longitude: 24.94189274671095),
CLLocationCoordinate2D(latitude: 60.17057890370404, longitude: 24.944958457828335),
CLLocationCoordinate2D(latitude: 60.17190499730512, longitude: 24.945178540794018),
CLLocationCoordinate2D(latitude: 60.172111309514946, longitude: 24.9469168488161)
]

private let route2 = [
CLLocationCoordinate2D(latitude: 60.17048155574875, longitude: 24.941910494113273),
CLLocationCoordinate2D(latitude: 60.170578004847926, longitude: 24.9449704567908),
CLLocationCoordinate2D(latitude: 60.17134054556911, longitude: 24.94735783361179),
CLLocationCoordinate2D(latitude: 60.17159974510375, longitude: 24.947563850901645)
]

private struct Tap {
var id = UUID()
var pos: CGPoint
var radius: Double?
}

@available(iOS 14.0, *)
private struct TapView: View {
var tap: Tap
var color: Color
@State var opacity = 1.0
var body: some View {
let r = tap.radius ?? 3
ZStack {
if tap.radius != nil {
Rectangle()
.stroke(color, lineWidth: 2) // Set border color and width
}

Circle()
.fill(color)
.frame(width: 6, height: 6)
}
.frame(width: r * 2, height: r * 2)
.offset(x: tap.pos.x - r, y: tap.pos.y - r)
.opacity(opacity)
.onAppear {
withAnimation(.easeInOut(duration: 2)) {
opacity = 0
}
}
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ In order to continue use them use the following import `@_spi(Experimental) impo
* Fix a crash on calling `LocationIndicatorLayer/location(coordinate:) function` due to missing 0 altitude value.
* Add a new Expression initializer `init(_ operator: Operator, _ arguments: ExpressionArgumentConvertible...)` to simplify the creation of expressions with multiple arguments.
That initializer doesn't require to wrap arguments in `Argument` cases. For example, `Exp(.eq, Exp(.get, "extrude"), "true")`.
* Add new experimental `radius` parameter to `TapInteraction`, `LongPressInteraction` and interaction managers to control the radius of a tappable area.

## 11.8.0 - 11 November, 2024

Expand Down
2 changes: 1 addition & 1 deletion MapboxMaps.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Pod::Spec.new do |m|
m.source_files = 'Sources/MapboxMaps/**/*.{swift,h}'
m.resource_bundles = { 'MapboxMapsResources' => ['Sources/**/*.{xcassets,strings}', 'Sources/MapboxMaps/MapboxMaps.json', 'Sources/MapboxMaps/PrivacyInfo.xcprivacy'] }

m.dependency 'MapboxCoreMaps', '11.9.0-SNAPSHOT.1112T1941Z.8130abf'
m.dependency 'MapboxCoreMaps', '11.9.0-SNAPSHOT.1122T1014Z.b9d442a'
m.dependency 'MapboxCommon', '24.9.0-SNAPSHOT.1112T0225Z.a361369'
m.dependency 'Turf', '4.0.0-beta.1'

Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import PackageDescription
import Foundation

let coreMaps = MapsDependency.coreMaps(version: "11.9.0-SNAPSHOT.1112T1941Z.8130abf", checksum: "0ef6877a48fbd153ab2d4f5605234d44b344d4089e5190f0d216bc79e8b55339")
let coreMaps = MapsDependency.coreMaps(version: "11.9.0-SNAPSHOT.1122T1014Z.b9d442a", checksum: "ec2407a32601b0fca1c36fffb10707214473091e8bab2992f74fd2185a480f83")
let common = MapsDependency.common(version: "24.9.0-SNAPSHOT.1112T0225Z.a361369", checksum: "e828f210cc591daf087206066d5117c0d4c10e8ec07c233ab1cd8d800a834a63")

let mapboxMapsPath: String? = nil
Expand Down
82 changes: 48 additions & 34 deletions Sources/MapboxMaps/Annotations/AnnotationManagerImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,48 +339,62 @@ final class AnnotationManagerImpl<AnnotationType: Annotation & AnnotationInterna
var dragTokens: TokenPair?
var clusterTokens: TokenPair?

var tapRadius: CGFloat? {
didSet { updateTapHandlers(force: tapRadius != oldValue) }
}

var longPressRadius: CGFloat? {
didSet { updateLongPressHandlers(force: longPressRadius != oldValue) }
}

private var handlesTaps = false {
didSet {
if handlesTaps {
if tapTokens == nil {
tapTokens = (
mapboxMap.addInteraction(tapInteraction(layerId: id)).erased,
mapboxMap.addInteraction(tapInteraction(layerId: dragId)).erased
)
}
} else {
tapTokens = nil
didSet { updateTapHandlers() }
}

private var handlesLongPress = false {
didSet { updateLongPressHandlers() }
}

private var handlesDrag = false {
didSet { updateDragHandlers() }
}

private func updateTapHandlers(force: Bool = false) {
if handlesTaps {
if tapTokens == nil || force {
tapTokens = (
mapboxMap.addInteraction(tapInteraction(layerId: id)).erased,
mapboxMap.addInteraction(tapInteraction(layerId: dragId)).erased
)
}
} else {
tapTokens = nil
}
}

private var handlesLongPress = false {
didSet {
if handlesLongPress {
if longPressTokens == nil {
longPressTokens = (
mapboxMap.addInteraction(longPressInteraction(layerId: id)).erased,
mapboxMap.addInteraction(longPressInteraction(layerId: dragId)).erased
)
}
} else {
longPressTokens = nil
private func updateLongPressHandlers(force: Bool = false) {
if handlesLongPress {
if longPressTokens == nil || force {
longPressTokens = (
mapboxMap.addInteraction(longPressInteraction(layerId: id)).erased,
mapboxMap.addInteraction(longPressInteraction(layerId: dragId)).erased
)
}
} else {
longPressTokens = nil
}
}

private var handlesDrag = false {
didSet {
if handlesDrag {
if dragTokens == nil {
dragTokens = (
mapboxMap.addInteraction(dragInteraction(layerId: id)).erased,
mapboxMap.addInteraction(dragInteraction(layerId: dragId)).erased
)
}
} else {
dragTokens = nil
private func updateDragHandlers(force: Bool = false) {
if handlesDrag {
if dragTokens == nil || force {
dragTokens = (
mapboxMap.addInteraction(dragInteraction(layerId: id)).erased,
mapboxMap.addInteraction(dragInteraction(layerId: dragId)).erased
)
}
} else {
dragTokens = nil
}
}

Expand All @@ -406,7 +420,7 @@ final class AnnotationManagerImpl<AnnotationType: Annotation & AnnotationInterna
}

private func tapInteraction(layerId: String) -> TapInteraction {
return TapInteraction(.layer(layerId)) { [weak self] feature, context in
return TapInteraction(.layer(layerId), radius: tapRadius) { [weak self] feature, context in
guard
let self,
let featureId = feature.id?.id else { return false }
Expand All @@ -430,7 +444,7 @@ final class AnnotationManagerImpl<AnnotationType: Annotation & AnnotationInterna
}

private func longPressInteraction(layerId: String) -> LongPressInteraction {
LongPressInteraction(.layer(layerId)) { [weak self] feature, context in
LongPressInteraction(.layer(layerId), radius: longPressRadius) { [weak self] feature, context in
self?.annotations.first { $0.id == feature.id?.id }?.longPressHandler?(context) ?? false
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e4f30f2

Please sign in to comment.