mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-01-19 07:16:57 +01:00
48 lines
1.0 KiB
Swift
Executable File
48 lines
1.0 KiB
Swift
Executable File
/*
|
||
See LICENSE folder for this sample’s licensing information.
|
||
|
||
Abstract:
|
||
A view wrapping a UIPageControl.
|
||
*/
|
||
|
||
import SwiftUI
|
||
|
||
import UIKit
|
||
|
||
struct PageControl: UIViewRepresentable {
|
||
var numberOfPages: Int
|
||
@Binding var currentPage: Int
|
||
|
||
func makeCoordinator() -> Coordinator {
|
||
Coordinator(self)
|
||
}
|
||
|
||
func makeUIView(context: Context) -> UIPageControl {
|
||
let control = UIPageControl()
|
||
control.numberOfPages = numberOfPages
|
||
control.addTarget(
|
||
context.coordinator,
|
||
action: #selector(Coordinator.updateCurrentPage(sender:)),
|
||
for: .valueChanged)
|
||
|
||
return control
|
||
}
|
||
|
||
func updateUIView(_ uiView: UIPageControl, context: Context) {
|
||
uiView.currentPage = currentPage
|
||
}
|
||
|
||
class Coordinator: NSObject {
|
||
var control: PageControl
|
||
|
||
init(_ control: PageControl) {
|
||
self.control = control
|
||
}
|
||
|
||
@objc
|
||
func updateCurrentPage(sender: UIPageControl) {
|
||
control.currentPage = sender.currentPage
|
||
}
|
||
}
|
||
}
|