Как добавить событие управления к пользовательской кнопке в RxCocoa? - PullRequest
1 голос
/ 13 июня 2019

Как добавить метод .tap к пользовательской кнопке, т.е.<myCustomButton>.rx.tap в RxSwift / RxCocoa, чтобы я мог связать нажатие кнопки с наблюдаемой.

CircularButton.swift

class UICircularButton: UIButton {
    override func layoutSubviews() {
        super.layoutSubviews()

        clipsToBounds = true
        subviews.first?.contentMode = .center

        let layer: CALayer = self.layer
        layer.cornerRadius = self.frame.size.width / 2
        layer.masksToBounds = true
    }
}

ViewController.swift

let transferButton: UIActionButton = {
        let button = UICircularButton(type: .system)
        button.setBackgroundImage(#imageLiteral(resourceName: "transfer"), for: .normal)
        button.backgroundColor = Colors.trueGreen
        return UIActionButton(button: button, actionLabel: "Transfer")
    }()

// Question
func configureBinding() {
        // How do I do this
        transferButton.rx.tap
            .bind(to: ...)
            .dispose(by: ...)
    }

Ответы [ 3 ]

2 голосов
/ 13 июня 2019

Вам не нужно его определять, он уже определен в UIButton, и ваш пользовательский класс наследует его.

1 голос
/ 13 июня 2019

Я обнаружил проблему, это моя ошибка, кнопка вложена в еще один слой, и из-за плохого именования я пропустил это.

0 голосов
/ 13 июня 2019

Вы можете назначить привязку внутри UIActionButton объявления следующим образом:

let transferButton: UIActionButton = {
    let button = UICircularButton(type: .system)
    button.setBackgroundImage(#imageLiteral(resourceName: "transfer"), for: .normal)
    button.backgroundColor = Colors.trueGreen
    button.rx.tap.bind {
        // My code or function call
    }.disposed(by: self.disposeBag)
    return UIActionButton(button: button, actionLabel: "Transfer")
}()
...