Jelajahi Sumber

Fix inset bottom double plus causing by pre-hidden

Fix inset bottom double plus causing by pre-hidden
MJLee 10 tahun lalu
induk
melakukan
e934e8e317

+ 1 - 1
MJRefresh.podspec

@@ -1,6 +1,6 @@
 Pod::Spec.new do |s|
   s.name         = "MJRefresh"
-  s.version      = "2.4.4"
+  s.version      = "2.4.5"
   s.summary      = "The easiest way to use pull-to-refresh"
   s.homepage     = "https://github.com/CoderMJLee/MJRefresh"
   s.license      = "MIT"

+ 27 - 7
MJRefresh/Base/MJRefreshAutoFooter.m

@@ -9,22 +9,40 @@
 #import "MJRefreshAutoFooter.h"
 
 @interface MJRefreshAutoFooter()
+@property (assign, nonatomic, getter=isPlusInsetBottom) BOOL plusInsetBottom;
+@property (assign, nonatomic, getter=isMinusInsetBottom) BOOL minusInsetBottom;
 @end
 
 @implementation MJRefreshAutoFooter
 
 #pragma mark - 初始化
+- (void)plusInsetBottom
+{
+    if (self.isPlusInsetBottom == NO) {
+        self.scrollView.mj_insetB += self.mj_h;
+        self.plusInsetBottom = YES;
+    }
+}
+
+- (void)minusInsetBottom
+{
+    if (self.isMinusInsetBottom == NO) {
+        self.scrollView.mj_insetB -= self.mj_h;
+        self.minusInsetBottom = YES;
+    }
+}
+
 - (void)willMoveToSuperview:(UIView *)newSuperview
 {
     [super willMoveToSuperview:newSuperview];
     
     if (newSuperview) { // 新的父控件
-        self.scrollView.mj_insetB += self.mj_h;
+        [self plusInsetBottom];
         
-        // 重新调整frame
-        [self scrollViewContentSizeDidChange:nil];
+        // 设置位置
+        self.mj_y = _scrollView.mj_contentH;
     } else { // 被移除了
-        self.scrollView.mj_insetB -= self.mj_h;
+        [self minusInsetBottom];
     }
 }
 
@@ -106,11 +124,13 @@ - (void)setHidden:(BOOL)hidden
     
     if (!lastHidden && hidden) {
         self.state = MJRefreshStateIdle;
-        _scrollView.mj_insetB -= self.mj_h;
+        
+        [self minusInsetBottom];
     } else if (lastHidden && !hidden) {
-        _scrollView.mj_insetB += self.mj_h;
+        [self plusInsetBottom];
         
-        [self scrollViewContentSizeDidChange:nil];
+        // 设置位置
+        self.mj_y = _scrollView.mj_contentH;
     }
 }
 @end

+ 2 - 22
MJRefresh/Base/MJRefreshBackFooter.m

@@ -92,14 +92,13 @@ - (void)setState:(MJRefreshState)state
         }
         
         CGFloat deltaH = [self heightForContentBreakView];
-        NSInteger currentCount = [self totalDataCountInScrollView];
         // 刚刷新完毕
-        if (MJRefreshStateRefreshing == oldState && deltaH > 0 && currentCount != self.lastRefreshCount) {
+        if (MJRefreshStateRefreshing == oldState && deltaH > 0 && self.scrollView.totalDataCount != self.lastRefreshCount) {
             self.scrollView.mj_offsetY = self.scrollView.mj_offsetY;
         }
     } else if (state == MJRefreshStateRefreshing) {
         // 记录刷新前的数量
-        self.lastRefreshCount = [self totalDataCountInScrollView];
+        self.lastRefreshCount = self.scrollView.totalDataCount;
         
         [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
             CGFloat bottom = self.mj_h + self.scrollViewOriginalInset.bottom;
@@ -146,23 +145,4 @@ - (CGFloat)happenOffsetY
         return - self.scrollViewOriginalInset.top;
     }
 }
-
-- (NSInteger)totalDataCountInScrollView
-{
-    NSInteger totalCount = 0;
-    if ([self.scrollView isKindOfClass:[UITableView class]]) {
-        UITableView *tableView = (UITableView *)self.scrollView;
-        
-        for (NSInteger section = 0; section<tableView.numberOfSections; section++) {
-            totalCount += [tableView numberOfRowsInSection:section];
-        }
-    } else if ([self.scrollView isKindOfClass:[UICollectionView class]]) {
-        UICollectionView *collectionView = (UICollectionView *)self.scrollView;
-        
-        for (NSInteger section = 0; section<collectionView.numberOfSections; section++) {
-            totalCount += [collectionView numberOfItemsInSection:section];
-        }
-    }
-    return totalCount;
-}
 @end

+ 2 - 17
MJRefresh/Base/MJRefreshFooter.m

@@ -45,23 +45,8 @@ - (void)scrollViewContentSizeDidChange:(NSDictionary *)change
 {
     [super scrollViewContentSizeDidChange:change];
     
-    // change == nil是第一次调用的时候
-    if (!self.isAutomaticallyHidden || change == nil) return;
-    
-    if ([self.scrollView isKindOfClass:[UITableView class]]) {
-        UITableView *tableView = (UITableView *)self.scrollView;
-        NSInteger count = 0;
-        for (NSInteger i = 0; i < tableView.numberOfSections; i++) {
-            count += [tableView numberOfRowsInSection:i];
-        }
-        self.hidden = (count == 0);
-    } else if ([self.scrollView isKindOfClass:[UICollectionView class]]) {
-        UICollectionView *collectionView = (UICollectionView *)self.scrollView;
-        NSInteger count = 0;
-        for (NSInteger i = 0; i < collectionView.numberOfSections; i++) {
-            count += [collectionView numberOfItemsInSection:i];
-        }
-        self.hidden = (count == 0);
+    if (self.isAutomaticallyHidden) {
+        self.hidden = (self.scrollView.totalDataCount == 0);
     }
 }
 

+ 2 - 0
MJRefresh/UIScrollView+MJExtension.h

@@ -20,4 +20,6 @@
 
 @property (assign, nonatomic) CGFloat mj_contentW;
 @property (assign, nonatomic) CGFloat mj_contentH;
+
+- (NSInteger)totalDataCount;
 @end

+ 19 - 0
MJRefresh/UIScrollView+MJExtension.m

@@ -107,4 +107,23 @@ - (CGFloat)mj_contentH
 {
     return self.contentSize.height;
 }
+
+- (NSInteger)totalDataCount
+{
+    NSInteger totalCount = 0;
+    if ([self isKindOfClass:[UITableView class]]) {
+        UITableView *tableView = (UITableView *)self;
+        
+        for (NSInteger section = 0; section<tableView.numberOfSections; section++) {
+            totalCount += [tableView numberOfRowsInSection:section];
+        }
+    } else if ([self isKindOfClass:[UICollectionView class]]) {
+        UICollectionView *collectionView = (UICollectionView *)self;
+        
+        for (NSInteger section = 0; section<collectionView.numberOfSections; section++) {
+            totalCount += [collectionView numberOfItemsInSection:section];
+        }
+    }
+    return totalCount;
+}
 @end

TEMPAT SAMPAH
MJRefreshExample/MJRefreshExample.xcodeproj/project.xcworkspace/xcuserdata/mj.xcuserdatad/UserInterfaceState.xcuserstate


+ 22 - 19
MJRefreshExample/MJRefreshExample/Classes/AppDelegate.m

@@ -10,34 +10,20 @@
 #import "AppDelegate.h"
 
 @interface AppDelegate ()
-//@property (strong, nonatomic) UIWindow *alert;
+@property (strong, nonatomic) UIWindow *alert;
 @end
 
 @implementation AppDelegate
 
 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-//    UIWindow *alert = [[UIWindow alloc] init];
-//    CGFloat width = 150;
-//    CGFloat x = [UIScreen mainScreen].bounds.size.width - width - 10;
-//    alert.frame = CGRectMake(x, 0, width, 25);
-//    alert.windowLevel = UIWindowLevelAlert;
-//    alert.hidden = NO;
-//    alert.alpha = 0.5;
-//    UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:@[@"示例1", @"示例2", @"示例3"]];
-//    control.tintColor = [UIColor orangeColor];
-//    control.frame = alert.bounds;
-//    control.selectedSegmentIndex = 0;
-//    [control addTarget:self action:@selector(contorlSelect:) forControlEvents:UIControlEventValueChanged];
-//    [alert addSubview:control];
-//    self.alert = alert;
     return YES;
 }
 
-//- (void)contorlSelect:(UISegmentedControl *)control
-//{
-//    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%zd", control.selectedSegmentIndex]];
-//}
+- (void)contorlSelect:(UISegmentedControl *)control
+{
+    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%zd", control.selectedSegmentIndex]];
+}
 
 - (void)applicationWillResignActive:(UIApplication *)application {
     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
@@ -55,6 +41,23 @@ - (void)applicationWillEnterForeground:(UIApplication *)application {
 
 - (void)applicationDidBecomeActive:(UIApplication *)application {
     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        UIWindow *alert = [[UIWindow alloc] init];
+        CGFloat width = 150;
+        CGFloat x = [UIScreen mainScreen].bounds.size.width - width - 10;
+        alert.frame = CGRectMake(x, 0, width, 25);
+        alert.windowLevel = UIWindowLevelAlert;
+        alert.hidden = NO;
+        alert.alpha = 0.5;
+        UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:@[@"示例1", @"示例2", @"示例3"]];
+        control.tintColor = [UIColor orangeColor];
+        control.frame = alert.bounds;
+        control.selectedSegmentIndex = 0;
+        [control addTarget:self action:@selector(contorlSelect:) forControlEvents:UIControlEventValueChanged];
+        [alert addSubview:control];
+        self.alert = alert;
+    });
 }
 
 - (void)applicationWillTerminate:(UIApplication *)application {

+ 2 - 1
MJRefreshExample/MJRefreshExample/Classes/MJSingleViewController.m

@@ -19,7 +19,7 @@ @implementation MJSingleViewController
 - (void)viewDidLoad {
     [super viewDidLoad];
     
-    self.count = 10;
+    self.count = 0;
     
     __weak typeof(self) weakSelf = self;
     __weak UITableView *tableView = self.tableView;
@@ -40,6 +40,7 @@ - (void)viewDidLoad {
             [tableView.footer endRefreshing];
         });
     }];
+    tableView.footer.automaticallyChangeAlpha = YES;
 }
 
 - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section