Skip to content

Commit

Permalink
Add an example text about a custom zoom control
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Petrov authored and ok-gurov committed May 6, 2021
1 parent 96bbe62 commit fcc8469
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class Container {
return sdk.sourceFactory
},
routeEditorSourceFactory: { [sdk = self.sdk] routeEditor in
return RouteEditorSource(context: sdk.context, routeEditor: routeEditor)
return sdk.sourceFactory.createRouteEditorSource(routeEditor: routeEditor)
},
routeEditorFactory: { [sdk = self.sdk] in
return RouteEditor(context: sdk.context)
Expand All @@ -52,8 +52,8 @@ final class Container {
mapFactory.mapView
},
customZoomControlFactory: {
[sdk = self.sdk] in
CustomZoomControl(map: sdk.map)
[mapFactory = self.mapFactory] in
CustomZoomControl(map: mapFactory.map)
},
mapControlFactory: self.mapFactory.mapControlFactory
)
Expand Down
2 changes: 1 addition & 1 deletion app/Map/CustomZoomControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public final class CustomZoomControl: UIControl {
private let stack: UIStackView

init(map: Map) {
let model = createZoomControlModel(map: map)
let model = ZoomControlModel(map: map)

let zoomIn = ZoomButton(model: model, direction: .zoomIn)
let zoomInImage = UIImage(systemName: "plus.magnifyingglass")
Expand Down
63 changes: 63 additions & 0 deletions docs/ru/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,66 @@ private func tap(point: ScreenPoint, tapRadius: ScreenDistance) {
self.getRenderedObjectsCancellable = cancel
}
```

## Создание кнопки управления масштабом

В SDK есть готовый к использованию блок управления масштабом, создаваемый
с помощью метода `PlatformSDK.Container.mapControlFactory.makeZoomControl()`.

Пример описывает создание собственной кнопки управления масштабом в фиксированном
направлении: `direction` указывает, будет кнопка уменьшать или увеличивать.
Ударживание кнопки в нажатом состоянии продолжает непрерывное изменение
масштаба. Когда предел будет достигнут — кнопка визуально изменит состояние.

Из двух таких кнопок можно собрать блок свободного управления масштабом.

Экземпляр `ZoomControlModel` можно получить с помощью функции
`PlatformSDK.createZoomControlModel(map:)`.

```
final class ZoomButton: UIButton {
private let model: ZoomControlModel
private let direction: ZoomControlButton
private var connection: ICancellable = NoopCancellable()
init(model: ZoomControlModel, direction: ZoomControlButton) {
self.model = model
self.direction = direction
super.init(frame: .zero)
self.addTarget(
self,
action: #selector(self.startZoom),
for: .touchDown
)
self.addTarget(
self,
action: #selector(self.stopZoom),
for: [.touchCancel, .touchUpInside, .touchUpOutside]
)
// Реагируем на отключение действия — выключаем кнопку.
// Такое возможно при достижении предела изменения масштаба.
self.connection = self.model.isEnabled(button: self.direction).sink {
[weak self] isEnabled in
DispatchQueue.main.async {
self?.isEnabled = isEnabled
}
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func startZoom() {
self.model.setPressed(button: self.direction, value: true)
}
@objc private func stopZoom() {
self.model.setPressed(button: self.direction, value: false)
}
}
```

0 comments on commit fcc8469

Please sign in to comment.