Repository navigation
[Work 39] 경로 지도의 참여자 마커 클러스터링과 초기 카메라 맞춤을 구현했습니다. #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4c586ed
REFACTOR: 지도 카메라 이동과 줌 기본값을 NMFNaverMapView 확장으로 공통화
snughnu 1b6e812
FEAT: 다중 좌표가 모두 보이도록 카메라를 맞추는 fit 유틸 추가
snughnu afeb2bb
FIX: 경로 화면 진입 시 참여자가 화면 밖에 있던 문제 해결
snughnu acb8415
FEAT: 화면 거리 기준 참여자 마커 클러스터링 계산부 추가
snughnu 3a73b35
REFACTOR: 경로 지도 참여자 마커를 식별자 기준으로 관리하도록 변경
snughnu 45cd9c1
FEAT: 참여자 클러스터 마커 오버레이 뷰 추가
snughnu 9e6f46b
FEAT: 경로 지도 카메라 정지 시 참여자 클러스터 계산
snughnu 140bf39
FEAT: 경로 지도에 참여자 클러스터 오버레이 표시
snughnu 022ef38
REFACTOR: 참여자 클러스터 표시를 UIView 오버레이에서 NMFMarker 이미지로 전환
snughnu 52b818e
FEAT: 참여자 클러스터 멤버를 2초 주기로 순회 표시
snughnu 5fac8a5
FEAT: 참여자 클러스터 전환 중간 프레임 렌더링 추가
snughnu d025b0d
FEAT: 참여자 클러스터 전환 슬라이드 애니메이션 추가
snughnu 44c4059
FIX: 클러스터·참여자 마커가 도착지 마커 위에 표시되도록 우선순위 지정
snughnu 2fed57e
REFACTOR: 클러스터 결과를 저장 프로퍼티 대신 인자로 전달
snughnu 6081f7b
REFACTOR: 클러스터 마커 관리와 순환·전환을 ClusterMarkerController로 분리
snughnu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
183 changes: 183 additions & 0 deletions
183
WhereAreYou/WhereAreYou/Presentation/Map/AppointmentRoute/ClusterMarkerController.swift
This file contains hidden or 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,183 @@ | ||
| // | ||
| // ClusterMarkerController.swift | ||
| // WhereAreYou | ||
| // | ||
| // Created by 김성훈 on 10/6/26. | ||
| // | ||
|
|
||
| import UIKit | ||
| import NMapsMap | ||
|
|
||
| /// 클러스터 마커의 표시, 멤버 순환, 슬라이드 전환 관리 | ||
| final class ClusterMarkerController: NSObject { | ||
|
|
||
| /// 지도에 표시된 클러스터 마커 — 멤버 구성이 같으면 재사용 | ||
| private struct ClusterMarker { | ||
| let memberIDs: [String] | ||
| let marker: NMFMarker | ||
| /// 멤버별 정지 이미지 — 전환 프레임의 재료 | ||
| let images: [UIImage] | ||
| let icons: [NMFOverlayImage] | ||
| var currentIndex = 0 | ||
| } | ||
|
|
||
| /// 클러스터 대표 멤버가 바뀌는 주기 | ||
| private static let rotationInterval: Duration = .seconds(2) | ||
| /// 대표 멤버가 바뀌는 슬라이드 전환 시간 | ||
| private static let transitionDuration: CFTimeInterval = 0.35 | ||
|
|
||
| private weak var mapView: NMFMapView? | ||
| private let zIndex: Int | ||
| private var clusterMarkers: [ClusterMarker] = [] | ||
| private var isActive = false | ||
| private var rotationTask: Task<Void, Never>? | ||
| private var transitionLink: CADisplayLink? | ||
| private var transitionStart: CFTimeInterval = 0 | ||
|
|
||
| init(mapView: NMFMapView, zIndex: Int) { | ||
| self.mapView = mapView | ||
| self.zIndex = zIndex | ||
| super.init() | ||
| } | ||
|
|
||
| // MARK: - Public | ||
|
|
||
| /// 클러스터 목록에 맞춰 마커를 갱신 — 멤버 구성이 같은 클러스터는 기존 마커 재사용 | ||
| func update(clusters: [ParticipantClusterer.Cluster], member: (String) -> ClusterMarkerView.Member?) { | ||
| var staleMarkers = clusterMarkers | ||
| clusterMarkers = clusters.map { cluster in | ||
| if let index = staleMarkers.firstIndex(where: { $0.memberIDs == cluster.memberIDs }) { | ||
| return staleMarkers.remove(at: index) | ||
| } | ||
| return makeClusterMarker(for: cluster, members: cluster.memberIDs.compactMap(member)) | ||
| } | ||
| staleMarkers.forEach { $0.marker.mapView = nil } | ||
| updateRotation() | ||
| } | ||
|
|
||
| /// 화면 표시 여부 — 표시 중일 때만 순환 동작 | ||
| func setActive(_ isActive: Bool) { | ||
| self.isActive = isActive | ||
| updateRotation() | ||
| } | ||
|
|
||
| func removeAll() { | ||
| clusterMarkers.forEach { $0.marker.mapView = nil } | ||
| clusterMarkers.removeAll() | ||
| updateRotation() | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // MARK: - Private | ||
|
|
||
| private extension ClusterMarkerController { | ||
|
|
||
| private func makeClusterMarker(for cluster: ParticipantClusterer.Cluster, members: [ClusterMarkerView.Member]) -> ClusterMarker { | ||
| let images = members.indices.map { index in | ||
| ClusterMarkerView.renderImage(members: members, memberIndex: index) | ||
| } | ||
| let icons = images.map { NMFOverlayImage(image: $0) } | ||
|
|
||
| let coordinate = cluster.coordinate | ||
| let marker = NMFMarker(position: NMGLatLng(lat: coordinate.latitude, lng: coordinate.longitude)) | ||
| if let firstIcon = icons.first { | ||
| marker.iconImage = firstIcon | ||
| } | ||
| marker.width = ClusterMarkerView.size.width | ||
| marker.height = ClusterMarkerView.size.height | ||
| marker.anchor = ClusterMarkerView.anchor | ||
| marker.zIndex = zIndex | ||
| marker.mapView = mapView | ||
|
|
||
| return ClusterMarker(memberIDs: cluster.memberIDs, marker: marker, images: images, icons: icons) | ||
| } | ||
|
|
||
| // MARK: - Rotation | ||
|
|
||
| /// 클러스터가 있고 화면에 떠 있을 때만 순환을 돌리고, 아니면 정지 | ||
| func updateRotation() { | ||
| guard !clusterMarkers.isEmpty, isActive else { | ||
| cancelTransition() | ||
| rotationTask?.cancel() | ||
| rotationTask = nil | ||
| return | ||
| } | ||
| guard rotationTask == nil else { return } | ||
|
|
||
| rotationTask = Task { [weak self] in | ||
| while !Task.isCancelled { | ||
| try? await Task.sleep(for: Self.rotationInterval) | ||
| guard !Task.isCancelled, let self else { return } | ||
| self.rotateMembers() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// 동작 줄이기가 켜져 있으면 바로 교체하고, 아니면 슬라이드 전환 | ||
| func rotateMembers() { | ||
| if UIAccessibility.isReduceMotionEnabled { | ||
| showNextMembers() | ||
| } else { | ||
| startTransition() | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Transition | ||
|
|
||
| func startTransition() { | ||
| guard transitionLink == nil else { return } | ||
|
|
||
| let link = CADisplayLink(target: self, selector: #selector(updateTransition)) | ||
| link.preferredFrameRateRange = CAFrameRateRange(minimum: 20, maximum: 30, preferred: 30) | ||
| link.add(to: .main, forMode: .common) | ||
| transitionLink = link | ||
| transitionStart = CACurrentMediaTime() | ||
| } | ||
|
|
||
| @objc func updateTransition() { | ||
| let elapsed = (CACurrentMediaTime() - transitionStart) / Self.transitionDuration | ||
| guard elapsed < 1 else { | ||
| finishTransition() | ||
| return | ||
| } | ||
|
|
||
| // 가속 후 감속 | ||
| let progress = elapsed * elapsed * (3 - 2 * elapsed) | ||
| for clusterMarker in clusterMarkers where clusterMarker.images.count > 1 { | ||
| let nextIndex = (clusterMarker.currentIndex + 1) % clusterMarker.images.count | ||
| let frame = ClusterMarkerView.renderTransitionFrame( | ||
| from: clusterMarker.images[clusterMarker.currentIndex], | ||
| to: clusterMarker.images[nextIndex], | ||
| progress: progress | ||
| ) | ||
| clusterMarker.marker.iconImage = NMFOverlayImage(image: frame) | ||
| } | ||
| } | ||
|
|
||
| /// 전환을 끝내고 다음 멤버로 확정 | ||
| func finishTransition() { | ||
| transitionLink?.invalidate() | ||
| transitionLink = nil | ||
| showNextMembers() | ||
| } | ||
|
|
||
| /// 전환을 중단하고 현재 멤버의 정지 아이콘으로 복원 | ||
| func cancelTransition() { | ||
| guard let link = transitionLink else { return } | ||
| link.invalidate() | ||
| transitionLink = nil | ||
| for clusterMarker in clusterMarkers where clusterMarker.icons.indices.contains(clusterMarker.currentIndex) { | ||
| clusterMarker.marker.iconImage = clusterMarker.icons[clusterMarker.currentIndex] | ||
| } | ||
| } | ||
|
|
||
| func showNextMembers() { | ||
| for index in clusterMarkers.indices where clusterMarkers[index].icons.count > 1 { | ||
| let nextIndex = (clusterMarkers[index].currentIndex + 1) % clusterMarkers[index].icons.count | ||
| clusterMarkers[index].currentIndex = nextIndex | ||
| clusterMarkers[index].marker.iconImage = clusterMarkers[index].icons[nextIndex] | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.