RxSwift必须结合 RxCocoa一起使用
一、导入头文件
import RxSwift
import RxCocoa
二、
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textFiled: UITextField!
@IBOutlet weak var button: UIButton!
@IBOutlet weak var scrollView: UIScrollView!
var person: Person = Person()
let disposeBag = DisposeBag()
var timer: Observable<Int>!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// setupNetwork()
// setupTimer()
// setupNotification()
// setupGestureRecognizer()
// setupScrollView()
// setupTextFiled()
// setupButton()
// setupKVO()
}
let disposeBag = DisposeBag() 下面的代码中 必须用disposeBag,直接用DisposeBag()就不会执行,或者干脆不要.disposed(by: disposeBag)也OK
基本使用一
// 1:创建序列
let ob = Observable<Any>.create { (observer) -> Disposable in
// 3:发送信号
observer.onNext("dyz")
observer.onCompleted() //RAC中不加这一句也可以 销毁,但是在RxSwift中必须加上这句,否则 无法销毁
observer.onError(NSError.init(domain: "dyzError", code: 10087, userInfo: nil)) //这样也可以销毁
// 4:销毁信号
return Disposables.create(with: {
print("销毁了")
})
}
// 2:订阅信号
let _ = ob.subscribe(onNext: { (text) in
print("订阅到:\(text)")
}, onError: { (error) in
print("error: \(error)")
}, onCompleted: {
print("完成")
}) {
print("销毁")
}
// let _ = ob.subscribe(onNext: { (text) in
// print("订阅到:\(text)")
// })
// 3:发送信号
// 4:销毁信号
RxSwift应用-KVO
监听的属性前 必须 加上 @objc dynamic
func setupKVO() {
self.person.rx.observeWeakly(String.self, "name").subscribe(onNext: { (value) in
print(value)
})
.disposed(by: disposeBag)
}
import UIKit
class Person: NSObject {
@objc dynamic var name: String = "dyz"
}
RxSwift应用-button响应
func setupButton() {
self.button.rx.tap
.subscribe(onNext: { () in
print("点击来了默认touchUpInside类型")
})
.disposed(by: disposeBag)
self.button.rx.controlEvent(.touchUpInside)
.subscribe(onNext: { () in
print("点击来了")
})
.disposed(by: disposeBag)
}
RxSwift应用-textfiled
func setupTextFiled() {
self.textFiled.rx.text.orEmpty.subscribe(onNext: { (text) in
print(text)
})
.disposed(by: disposeBag)
//绑定
self.textFiled.rx.text
.bind(to: self.button.rx.title())
.disposed(by: disposeBag)
}
RxSwift应用-scrollView
func setupScrollView() {
scrollView.rx.contentOffset.subscribe(onNext: { [weak self](content) in
self?.view.backgroundColor = UIColor.init(red: content.y/255*0.8, green: content.y/255*0.6, blue: content.y/255*0.3, alpha: 1)
})
.disposed(by: disposeBag)
}
RxSwift应用-手势
func setupGestureRecognizer() {
let tap = UITapGestureRecognizer()
self.label.addGestureRecognizer(tap)
self.label.isUserInteractionEnabled = true
tap.rx.event.subscribe(onNext: { (tap) in
print(tap.view!)
})
.disposed(by: disposeBag)
}
RxSwift应用-通知
func setupNotification() {
NotificationCenter.default.rx.notification(UIResponder.keyboardWillShowNotification)
.subscribe(onNext: { (noti) in
print(noti)
})
.disposed(by: disposeBag)
}
RxSwift应用-timer定时器
func setupTimer() {
timer = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
timer.subscribe(onNext: { (num) in
print(num)
})
.disposed(by: disposeBag)
}
RxSwift应用-网络请求
func setupNetwork() {
let url = URL(string: "https://www.baidu.com")
// URLSession.shared.dataTask(with: url!) { (data, response, error) in
// print(String.init(data: data!, encoding: .utf8)!)
// }.resume()
URLSession.shared.rx.response(request: URLRequest(url: url!)).subscribe(onNext: { (response: HTTPURLResponse, data: Data) in
print(response)
}).disposed(by: disposeBag)
}
网友评论