# #import UIKit
enum RefreshStateType: Int {
case Normal = 0
case Pull = 1
case Refreshing = 2
}
class refresh: UIControl {
private lazy var stateLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFontOfSize(16)
label.text = "正常"
label.textColor = UIColor.redColor()
return label
}()
private lazy var jiantouImg: UIImageView = UIImageView(image: UIImage(named: "tableview_pull_refresh"))
private lazy var indicator: UIActivityIndicatorView = {
let indi = UIActivityIndicatorView(activityIndicatorStyle: .White)
indi.color = UIColor.redColor()
return indi
}()
override init(frame: CGRect) {
super.init(frame: CGRectMake(0, -50, UIScreen.mainScreen().bounds.width, 50))
backgroundColor = UIColor.brownColor()
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupUI() {
addSubview(stateLabel)
addSubview(jiantouImg)
addSubview(indicator)
stateLabel.translatesAutoresizingMaskIntoConstraints = false
addConstraint(NSLayoutConstraint(item: stateLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: stateLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
jiantouImg.translatesAutoresizingMaskIntoConstraints = false
addConstraint(NSLayoutConstraint(item: jiantouImg, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: -35))
addConstraint(NSLayoutConstraint(item: jiantouImg, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
indicator.translatesAutoresizingMaskIntoConstraints = false
addConstraint(NSLayoutConstraint(item: indicator, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: -35))
addConstraint(NSLayoutConstraint(item: indicator, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
}
var scrollView: UIScrollView?
override func willMoveToSuperview(newSuperview: UIView?) {
guard let scrollView = newSuperview as? UIScrollView else {
return
}
self.scrollView = scrollView
self.scrollView?.addObserver(self, forKeyPath: "contentOffset", options: .New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
getRefreshStates((self.scrollView?.contentOffset.y) ?? 0)
}
var states: RefreshStateType = .Normal{
didSet{
switch states {
case .Normal:
self.stateLabel.text = "正常"
UIView.animateWithDuration(0.25, animations: {
self.jiantouImg.transform = CGAffineTransformIdentity
})
if oldValue == .Refreshing {
UIView.animateWithDuration(0.25, animations: {
self.scrollView?.contentInset.top -= 50
}, completion: { (_) in
self.jiantouImg.hidden = false
self.indicator.stopAnimating()
})
}
case .Pull:
self.stateLabel.text = "下拉"
UIView.animateWithDuration(0.25, animations: {
self.jiantouImg.transform = CGAffineTransformMakeRotation(CGFloat(-3*M_PI))
})
case .Refreshing:
self.stateLabel.text = "刷新中"
self.jiantouImg.hidden = true
self.indicator.startAnimating()
UIView.animateWithDuration(0.25, animations: {
self.scrollView?.contentInset.top += 50
}, completion: { (_) in
self.sendActionsForControlEvents(.ValueChanged)
})
}
}
}
func endRefresh() {
states = .Normal
}
func getRefreshStates(contentOffsety: CGFloat) {
let contentTop = self.scrollView?.contentInset.top ?? 0
if (self.scrollView!.dragging) {
if contentOffsety <= -contentTop-50 && states == .Normal{
states = .Pull
}else if contentOffsety > -contentTop-50 && states == .Pull{
states = .Normal
}
}else{
if states == .Pull {
states = .Refreshing
}
}
}
deinit{
self.scrollView?.removeObserver(self, forKeyPath: "contentOffset", context: nil)
}
}
自定义 <刷新>功能
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 1.背景 无论是 Androi 还是 ios,下拉刷新都是一个很有必要也很重要的功能。那么在 RN(以下用 RN ...
- 绪论 最近项目里面用到了下拉刷新和左滑删除,网上找了找并没有可以用的,有比较好的左滑删除,但是并没有和下拉刷新上拉...