XDRefresh
開(kāi)發(fā)的時(shí)候經(jīng)常用到上下拉刷新控件撤奸,因?yàn)轫?xiàng)目只是需要實(shí)現(xiàn)簡(jiǎn)單的菊花樣式的刷新動(dòng)畫(huà)滔韵,所以自己封裝了一個(gè)簡(jiǎn)易版上下拉刷新控件,到時(shí)未來(lái)再拓展出可以自定義的刷新控件。
效果圖
使用
#import "KitRefresh.h"
...
KitRefreshHeader *_header;
KitRefreshFooter *_footer;
_header = [KitRefreshHeader headerOfScrollView:tableView withRefreshingBlock:^{
//下拉刷新回調(diào)
}];
_footer = [KitRefreshFooter footerOfScrollView:tableView withRefreshingBlock:^{
//上拉刷新回調(diào)
}];
實(shí)現(xiàn)原理
通過(guò)觀察scrollview的contentOffset屬性的變化來(lái)進(jìn)行處理冗澈。
給出一段KVO處理馏颂,具體實(shí)現(xiàn)大家可以看源碼
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSString *,id> *)change
context:(void *)context {
if ([keyPath isEqualToString:@"contentOffset"]) {
_contentHeight = _scrollView.contentSize.height;
//拉伸狀態(tài)
if (_scrollView.isDragging) {
CGFloat currentPosition = _scrollView.contentOffset.y;
if (!_isRefreshing) {
[UIView animateWithDuration:0.3f animations:^{
//下拉過(guò)程超過(guò)_headerHeight*1.5
if (currentPosition < -_headerHeight* 1.5 ) {
_statusLabel.text = _releaseText;
_arrowView.transform = CGAffineTransformMakeRotation(M_PI);
}else {
//上拉
if (currentPosition - _lastPosition > 5) {
_lastPosition = currentPosition;
_statusLabel.text = _pulldownText;
_arrowView.transform = CGAffineTransformMakeRotation(M_PI*2);
//下拉不超過(guò)_headerHeight*1.5
}else if(_lastPosition - currentPosition > 5) {
_lastPosition = currentPosition;
}
}
}];
}
}else {
//松開(kāi)手時(shí)
if ([_statusLabel.text isEqualToString:_releaseText]) {
[self beginRefreshing];
}
}
}
}
源碼地址
https://github.com/caixindong/XDRefresh/tree/master/XDRefresh
大家覺(jué)得喜歡就賞個(gè)star示血,有什么問(wèn)題可以issue我或者在評(píng)論指出,相互學(xué)習(xí)