直播平台源码,自定义下拉刷新控件
import Foundation
import UIKit
import SnapKit
class XRefreshControl: UIRefreshControl {
var observation: NSKeyValueObservation?
var isLocalRefreshing: Bool = false
let indicator = UIProgressView(progressViewStyle: .bar)
var refreshingBlock: (()->Void)?
override init(frame: CGRect) {
super.init(frame: frame)
observation = observe(
\.frame,
options: .new
) { [weak self] object, change in
if self?.isRefreshing == true {
if self?.isLocalRefreshing == false {
if self?.refreshingBlock != nil {
self?.refreshingBlock!()
}
}
self?.isLocalRefreshing = true
} else {
let height = change.newValue!.height
self?.indicator.progress = min(Float(abs(height / 60)), 1)
}
}
}
convenience init(refreshingBlock: @escaping ()->Void) {
self.init(frame: .zero)
self.refreshingBlock = refreshingBlock
self.layer.masksToBounds = true
self.backgroundColor = .red
let v = UIView()
v.backgroundColor = .red
let center = UIView()
v.addSubview(center)
let title = UILabel()
title.text = "加载中"
title.textColor = .black
center.addSubview(title)
indicator.layer.masksToBounds = true
center.addSubview(indicator)
self.addSubview(v)
v.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
center.snp.makeConstraints { make in
make.center.equalToSuperview()
make.width.equalToSuperview()
}
indicator.snp.makeConstraints { make in
make.top.equalToSuperview()
make.width.height.equalTo(32)
make.centerX.equalToSuperview()
}
title.snp.makeConstraints { make in
make.top.equalTo(indicator.snp.bottom)
make.bottom.equalToSuperview()
make.centerX.equalToSuperview()
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
observation = nil
}
override func endRefreshing() {
super.endRefreshing()
self.isLocalRefreshing = false
indicator.progress = 0
}
}
extension UITableView {
func endRefreshing() {
if ((self.refreshControl?.isKind(of: XRefreshControl.self)) != nil) {
self.refreshControl?.endRefreshing()
}
}
}
以上就是直播平台源码,自定义下拉刷新控件, 更多内容欢迎关注之后的文章
标签:控件,indicator,自定义,self,refreshingBlock,init,源码,make,equalToSuperview From: https://www.cnblogs.com/yunbaomengnan/p/17802972.html