Skip to content

Commit

Permalink
增加小功能
Browse files Browse the repository at this point in the history
1.【开始刷新】完成后的回调
2.【结束刷新】完成后的回调
  • Loading branch information
520dev committed Jun 16, 2016
1 parent 4678b48 commit 76f979e
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 56 deletions.
2 changes: 1 addition & 1 deletion MJRefresh.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'MJRefresh'
s.version = '3.1.6'
s.version = '3.1.7'
s.summary = 'An easy way to use pull-to-refresh'
s.homepage = 'https://github.com/CoderMJLee/MJRefresh'
s.license = 'MIT'
Expand Down
6 changes: 6 additions & 0 deletions MJRefresh/Base/MJRefreshAutoFooter.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ - (void)setState:(MJRefreshState)state
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self executeRefreshingCallback];
});
} else if (state == MJRefreshStateNoMoreData || state == MJRefreshStateIdle) {
if (MJRefreshStateRefreshing == oldState) {
if (self.endRefreshingCompletionBlock) {
self.endRefreshingCompletionBlock();
}
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions MJRefresh/Base/MJRefreshBackFooter.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ - (void)setState:(MJRefreshState)state
if (self.isAutomaticallyChangeAlpha) self.alpha = 0.0;
} completion:^(BOOL finished) {
self.pullingPercent = 0.0;

if (self.endRefreshingCompletionBlock) {
self.endRefreshingCompletionBlock();
}
}];
}

Expand Down
13 changes: 11 additions & 2 deletions MJRefresh/Base/MJRefreshComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "UIView+MJExtension.h"
#import "UIScrollView+MJExtension.h"
#import "UIScrollView+MJRefresh.h"
#import "NSBundle+MJRefresh.h"

/** 刷新控件的状态 */
typedef NS_ENUM(NSInteger, MJRefreshState) {
Expand All @@ -29,6 +30,10 @@ typedef NS_ENUM(NSInteger, MJRefreshState) {

/** 进入刷新状态的回调 */
typedef void (^MJRefreshComponentRefreshingBlock)();
/** 开始刷新后的回调(进入刷新状态后的回调) */
typedef void (^MJRefreshComponentbeginRefreshingCompletionBlock)();
/** 结束刷新后的回调 */
typedef void (^MJRefreshComponentEndRefreshingCompletionBlock)();

/** 刷新控件的基类 */
@interface MJRefreshComponent : UIView
Expand All @@ -44,8 +49,6 @@ typedef void (^MJRefreshComponentRefreshingBlock)();
/** 设置回调对象和回调方法 */
- (void)setRefreshingTarget:(id)target refreshingAction:(SEL)action;

- (NSString *)localizedStringForKey:(NSString *)key;

/** 回调对象 */
@property (weak, nonatomic) id refreshingTarget;
/** 回调方法 */
Expand All @@ -56,8 +59,14 @@ typedef void (^MJRefreshComponentRefreshingBlock)();
#pragma mark - 刷新状态控制
/** 进入刷新状态 */
- (void)beginRefreshing;
- (void)beginRefreshingWithCompletionBlock:(void (^)())completionBlock;
/** 开始刷新后的回调(进入刷新状态后的回调) */
@property (copy, nonatomic) MJRefreshComponentbeginRefreshingCompletionBlock beginRefreshingCompletionBlock;
/** 结束刷新的回调 */
@property (copy, nonatomic) MJRefreshComponentEndRefreshingCompletionBlock endRefreshingCompletionBlock;
/** 结束刷新状态 */
- (void)endRefreshing;
- (void)endRefreshingWithCompletionBlock:(void (^)())completionBlock;
/** 是否正在刷新 */
- (BOOL)isRefreshing;
/** 刷新状态 一般交给子类内部实现 */
Expand Down
56 changes: 17 additions & 39 deletions MJRefresh/Base/MJRefreshComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

#import "MJRefreshComponent.h"
#import "MJRefreshConst.h"
#import "UIView+MJExtension.h"
#import "UIScrollView+MJRefresh.h"
#import "NSBundle+MJRefresh.h"

@interface MJRefreshComponent()
@property (strong, nonatomic) UIPanGestureRecognizer *pan;
Expand Down Expand Up @@ -134,42 +131,6 @@ - (void)setRefreshingTarget:(id)target refreshingAction:(SEL)action
self.refreshingAction = action;
}

- (NSString *)localizedStringForKey:(NSString *)key{
return [self localizedStringForKey:key withDefault:nil];
}

- (NSString *)localizedStringForKey:(NSString *)key withDefault:(NSString *)defaultString
{
static NSBundle *bundle = nil;
if (bundle == nil) {
// 获得设备的语言
NSString *language = [NSLocale preferredLanguages].firstObject;
// 如果是iOS9以上,去掉后面的设备购买地区比如zh-Hans-US和zh-Hans-CN后面的US和CN
if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {
NSRange range = [language rangeOfString:@"-" options:NSBackwardsSearch];
if (range.location != NSNotFound) {
language = [language substringToIndex:range.location];
}
}

if ([language isEqualToString:@"zh"]) { // zh-HK被去掉了-HK
language = @"zh-Hant";
}

if (language.length == 0) {
language = @"zh-Hans";
}

// 先从MJRefresh.bundle中查找资源
NSBundle *refreshBundle = [NSBundle mj_refreshBundle];
if ([refreshBundle.localizations containsObject:language]) {
bundle = [NSBundle bundleWithPath:[refreshBundle pathForResource:language ofType:@"lproj"]];
}
}
defaultString = [bundle localizedStringForKey:key value:defaultString table:nil];
return [[NSBundle mainBundle] localizedStringForKey:key value:defaultString table:nil];
}

- (void)setState:(MJRefreshState)state
{
_state = state;
Expand Down Expand Up @@ -199,12 +160,26 @@ - (void)beginRefreshing
}
}

- (void)beginRefreshingWithCompletionBlock:(void (^)())completionBlock
{
self.beginRefreshingCompletionBlock = completionBlock;

[self beginRefreshing];
}

#pragma mark 结束刷新状态
- (void)endRefreshing
{
self.state = MJRefreshStateIdle;
}

- (void)endRefreshingWithCompletionBlock:(void (^)())completionBlock
{
self.endRefreshingCompletionBlock = completionBlock;

[self endRefreshing];
}

#pragma mark 是否正在刷新
- (BOOL)isRefreshing
{
Expand Down Expand Up @@ -257,6 +232,9 @@ - (void)executeRefreshingCallback
if ([self.refreshingTarget respondsToSelector:self.refreshingAction]) {
MJRefreshMsgSend(MJRefreshMsgTarget(self.refreshingTarget), self.refreshingAction, self);
}
if (self.beginRefreshingCompletionBlock) {
self.beginRefreshingCompletionBlock();
}
});
}
@end
Expand Down
4 changes: 4 additions & 0 deletions MJRefresh/Base/MJRefreshHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ - (void)setState:(MJRefreshState)state
if (self.isAutomaticallyChangeAlpha) self.alpha = 0.0;
} completion:^(BOOL finished) {
self.pullingPercent = 0.0;

if (self.endRefreshingCompletionBlock) {
self.endRefreshingCompletionBlock();
}
}];
} else if (state == MJRefreshStateRefreshing) {
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down
6 changes: 3 additions & 3 deletions MJRefresh/Custom/Footer/Auto/MJRefreshAutoStateFooter.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ - (void)prepare
[super prepare];

// 初始化文字
[self setTitle:[self localizedStringForKey:MJRefreshAutoFooterIdleText] forState:MJRefreshStateIdle];
[self setTitle:[self localizedStringForKey:MJRefreshAutoFooterRefreshingText] forState:MJRefreshStateRefreshing];
[self setTitle:[self localizedStringForKey:MJRefreshAutoFooterNoMoreDataText] forState:MJRefreshStateNoMoreData];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshAutoFooterIdleText] forState:MJRefreshStateIdle];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshAutoFooterRefreshingText] forState:MJRefreshStateRefreshing];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshAutoFooterNoMoreDataText] forState:MJRefreshStateNoMoreData];

// 监听label
self.stateLabel.userInteractionEnabled = YES;
Expand Down
8 changes: 4 additions & 4 deletions MJRefresh/Custom/Footer/Back/MJRefreshBackStateFooter.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ - (void)prepare
[super prepare];

// 初始化文字
[self setTitle:[self localizedStringForKey:MJRefreshBackFooterIdleText] forState:MJRefreshStateIdle];
[self setTitle:[self localizedStringForKey:MJRefreshBackFooterPullingText] forState:MJRefreshStatePulling];
[self setTitle:[self localizedStringForKey:MJRefreshBackFooterRefreshingText] forState:MJRefreshStateRefreshing];
[self setTitle:[self localizedStringForKey:MJRefreshBackFooterNoMoreDataText] forState:MJRefreshStateNoMoreData];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshBackFooterIdleText] forState:MJRefreshStateIdle];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshBackFooterPullingText] forState:MJRefreshStatePulling];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshBackFooterRefreshingText] forState:MJRefreshStateRefreshing];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshBackFooterNoMoreDataText] forState:MJRefreshStateNoMoreData];
}

- (void)placeSubviews
Expand Down
14 changes: 7 additions & 7 deletions MJRefresh/Custom/Header/MJRefreshStateHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ - (void)setLastUpdatedTimeKey:(NSString *)lastUpdatedTimeKey

// 3.显示日期
self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@%@%@",
[self localizedStringForKey:MJRefreshHeaderLastTimeText],
isToday ? [self localizedStringForKey:MJRefreshHeaderDateTodayText] : @"",
[NSBundle mj_localizedStringForKey:MJRefreshHeaderLastTimeText],
isToday ? [NSBundle mj_localizedStringForKey:MJRefreshHeaderDateTodayText] : @"",
time];
} else {
self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@%@",
[self localizedStringForKey:MJRefreshHeaderLastTimeText],
[self localizedStringForKey:MJRefreshHeaderNoneLastDateText]];
[NSBundle mj_localizedStringForKey:MJRefreshHeaderLastTimeText],
[NSBundle mj_localizedStringForKey:MJRefreshHeaderNoneLastDateText]];
}
}

Expand All @@ -115,9 +115,9 @@ - (void)prepare
[super prepare];

// 初始化文字
[self setTitle:[self localizedStringForKey:MJRefreshHeaderIdleText] forState:MJRefreshStateIdle];
[self setTitle:[self localizedStringForKey:MJRefreshHeaderPullingText] forState:MJRefreshStatePulling];
[self setTitle:[self localizedStringForKey:MJRefreshHeaderRefreshingText] forState:MJRefreshStateRefreshing];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshHeaderIdleText] forState:MJRefreshStateIdle];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshHeaderPullingText] forState:MJRefreshStatePulling];
[self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshHeaderRefreshingText] forState:MJRefreshStateRefreshing];
}

- (void)placeSubviews
Expand Down
2 changes: 2 additions & 0 deletions MJRefresh/NSBundle+MJRefresh.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
@interface NSBundle (MJRefresh)
+ (instancetype)mj_refreshBundle;
+ (UIImage *)mj_arrowImage;
+ (NSString *)mj_localizedStringForKey:(NSString *)key value:(NSString *)value;
+ (NSString *)mj_localizedStringForKey:(NSString *)key;
@end
37 changes: 37 additions & 0 deletions MJRefresh/NSBundle+MJRefresh.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,41 @@ + (UIImage *)mj_arrowImage
}
return arrowImage;
}

+ (NSString *)mj_localizedStringForKey:(NSString *)key
{
return [self mj_localizedStringForKey:key value:nil];
}

+ (NSString *)mj_localizedStringForKey:(NSString *)key value:(NSString *)value
{
static NSBundle *bundle = nil;
if (bundle == nil) {
// 获得设备的语言
NSString *language = [NSLocale preferredLanguages].firstObject;
// 如果是iOS9以上,去掉后面的设备购买地区比如zh-Hans-US和zh-Hans-CN后面的US和CN
if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {
NSRange range = [language rangeOfString:@"-" options:NSBackwardsSearch];
if (range.location != NSNotFound) {
language = [language substringToIndex:range.location];
}
}

if ([language isEqualToString:@"zh"]) { // zh-HK被去掉了-HK
language = @"zh-Hant";
}

if (language.length == 0) {
language = @"zh-Hans";
}

// 先从MJRefresh.bundle中查找资源
NSBundle *refreshBundle = [NSBundle mj_refreshBundle];
if ([refreshBundle.localizations containsObject:language]) {
bundle = [NSBundle bundleWithPath:[refreshBundle pathForResource:language ofType:@"lproj"]];
}
}
value = [bundle localizedStringForKey:key value:value table:nil];
return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
}
@end

0 comments on commit 76f979e

Please sign in to comment.