|
- import Combine
- import UIKit
-
- // MARK: - Ядро-VC
-
- extension Main {
- class Core: UIViewController {
- let didSelectButton = PassthroughSubject<Int, Never>()
- var buttons = [UIButton]()
- var subscriptions = [AnyCancellable]()
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- view.backgroundColor = .white
-
- // Создаём 16 кнопок.
- for i in 0..<16 {
- let btn = UIButton()
- btn.tag = i
- view.addSubview(btn)
- buttons.append(btn)
- }
-
- // Расставляем кнопки в сетке 4x4.
- for (id, p) in memoryItemPositions(c: M()).enumerated() {
- let btn = buttons[id]
- btn.frame =
- CGRect(
- x: CGFloat(p.x),
- y: CGFloat(p.y),
- width: CGFloat(memorySide()),
- height: CGFloat(memorySide())
- )
- btn.backgroundColor = .blue
- }
-
- // Учитываем нажатия кнопок.
- for btn in buttons {
- btn.addTarget(self, action: #selector(Main.Core.selectButton), for: .touchUpInside)
- }
-
-
- let groups = memoryGroups(c: M())
-
- // Выводим номер нажатой кнопки.
- didSelectButton
- .sink { i in
- print("ИГР MainP.viewDL didSB id/group: '\(i)'/'\(groups[Float(i)])'")
- }
- .store(in: &subscriptions)
- }
-
- @objc func selectButton(_ btn: UIButton) {
- didSelectButton.send(btn.tag)
- }
- }
- }
|