Browse Source

Add automaticallyHidden property to MJRefreshAutoFooter

MJRefreshAutoFooter can be automatically hidden when the count of data
is 0.
MJLee 10 years ago
parent
commit
fd07d54273

+ 1 - 1
MJRefresh.podspec

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

+ 3 - 0
MJRefresh/Base/MJRefreshAutoFooter.h

@@ -14,4 +14,7 @@
 
 /** 当底部控件出现多少时就自动刷新(默认为1.0,也就是底部控件完全出现时,才会自动刷新) */
 @property (assign, nonatomic) CGFloat appearencePercentTriggerAutoRefresh;
+
+/** 自动根据有无数据来显示和隐藏(有数据就显示,没有数据隐藏) */
+@property (assign, nonatomic) BOOL automaticallyHidden;
 @end

+ 21 - 0
MJRefresh/Base/MJRefreshAutoFooter.m

@@ -39,6 +39,9 @@ - (void)prepare
     
     // 设置为默认状态
     self.automaticallyRefresh = YES;
+    
+    // 默认是自动隐藏
+    self.automaticallyHidden = YES;
 }
 
 - (void)scrollViewContentSizeDidChange:(NSDictionary *)change
@@ -47,6 +50,24 @@ - (void)scrollViewContentSizeDidChange:(NSDictionary *)change
     
     // 设置位置
     self.mj_y = _scrollView.mj_contentH;
+    
+    if ([self.scrollView isKindOfClass:[UITableView class]]) {
+        UITableView *tableView = (UITableView *)self.scrollView;
+        NSInteger count = 0;
+        NSInteger sections = [tableView numberOfSections];
+        for (NSInteger i = 0; i < sections; i++) {
+            count += [tableView numberOfRowsInSection:i];
+        }
+        self.hidden = (count == 0);
+    } else if ([self.scrollView isKindOfClass:[UICollectionView class]]) {
+        UICollectionView *collectionView = (UICollectionView *)self.scrollView;
+        NSInteger count = 0;
+        NSInteger sections = [collectionView numberOfSections];
+        for (NSInteger i = 0; i < sections; i++) {
+            count += [collectionView numberOfItemsInSection:i];
+        }
+        self.hidden = (count == 0);
+    }
 }
 
 - (void)scrollViewContentOffsetDidChange:(NSDictionary *)change

+ 1 - 1
MJRefresh/Base/MJRefreshBackFooter.m

@@ -85,7 +85,7 @@ - (void)setState:(MJRefreshState)state
                 self.scrollView.mj_insetB -= self.lastBottomDelta;
                 
                 // 自动调整透明度
-                if (self.isAutoChangeAlpha) self.alpha = 0.0;
+                if (self.isAutomaticallyChangeAlpha) self.alpha = 0.0;
             } completion:^(BOOL finished) {
                 self.pullingPercent = 0.0;
             }];

+ 3 - 1
MJRefresh/Base/MJRefreshComponent.h

@@ -83,7 +83,9 @@ typedef void (^MJRefreshComponentRefreshingBlock)();
 /** 拉拽的百分比(交给子类重写) */
 @property (assign, nonatomic) CGFloat pullingPercent;
 /** 根据拖拽比例自动切换透明度 */
-@property (assign, nonatomic, getter=isAutoChangeAlpha) BOOL autoChangeAlpha;
+@property (assign, nonatomic, getter=isAutoChangeAlpha) BOOL autoChangeAlpha MJRefreshDeprecated("请使用automaticallyChangeAlpha属性");
+/** 根据拖拽比例自动切换透明度 */
+@property (assign, nonatomic, getter=isAutomaticallyChangeAlpha) BOOL automaticallyChangeAlpha;
 @end
 
 @interface UILabel(MJRefresh)

+ 10 - 4
MJRefresh/Base/MJRefreshComponent.m

@@ -162,12 +162,18 @@ - (BOOL)isRefreshing
 #pragma mark 自动切换透明度
 - (void)setAutoChangeAlpha:(BOOL)autoChangeAlpha
 {
-    _autoChangeAlpha = autoChangeAlpha;
+    self.automaticallyChangeAlpha = autoChangeAlpha;
+}
+
+- (void)setAutomaticallyChangeAlpha:(BOOL)automaticallyChangeAlpha
+{
+    _automaticallyChangeAlpha = automaticallyChangeAlpha;
+    
     
     if (self.isRefreshing) return;
     
-    if (autoChangeAlpha) {
-         self.alpha = self.pullingPercent;
+    if (automaticallyChangeAlpha) {
+        self.alpha = self.pullingPercent;
     } else {
         self.alpha = 1.0;
     }
@@ -180,7 +186,7 @@ - (void)setPullingPercent:(CGFloat)pullingPercent
     
     if (self.isRefreshing) return;
     
-    if (self.isAutoChangeAlpha) {
+    if (self.isAutomaticallyChangeAlpha) {
         self.alpha = pullingPercent;
     }
 }

+ 1 - 1
MJRefresh/Base/MJRefreshHeader.m

@@ -106,7 +106,7 @@ - (void)setState:(MJRefreshState)state
             self.scrollView.mj_insetT -= self.mj_h;
             
             // 自动调整透明度
-            if (self.isAutoChangeAlpha) self.alpha = 0.0;
+            if (self.isAutomaticallyChangeAlpha) self.alpha = 0.0;
         } completion:^(BOOL finished) {
             self.pullingPercent = 0.0;
         }];

BIN
MJRefreshExample/MJRefreshExample.xcodeproj/project.xcworkspace/xcuserdata/mj.xcuserdatad/UserInterfaceState.xcuserstate


+ 1 - 1
MJRefreshExample/MJRefreshExample/Classes/MJExampleViewController.m

@@ -74,7 +74,7 @@ - (void)viewDidLoad
     }];
     
     // 设置自动切换透明度(在导航栏下面自动隐藏)
-    tableView.header.autoChangeAlpha = YES;
+    tableView.header.automaticallyChangeAlpha = YES;
     
     // 上拉刷新
     tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{

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

@@ -31,7 +31,7 @@ - (void)viewDidLoad {
             [tableView.header endRefreshing];
         });
     }];
-    tableView.header.autoChangeAlpha = YES;
+    tableView.header.automaticallyChangeAlpha = YES;
     
     tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@@ -40,7 +40,7 @@ - (void)viewDidLoad {
             [tableView.footer endRefreshing];
         });
     }];
-    tableView.footer.autoChangeAlpha = YES;
+    tableView.footer.automaticallyChangeAlpha = YES;
 }
 
 - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section

+ 5 - 2
MJRefreshExample/MJRefreshExample/Classes/MJTableViewController.m

@@ -65,7 +65,7 @@ - (void)example03
     MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
     
     // 设置自动切换透明度(在导航栏下面自动隐藏)
-    header.autoChangeAlpha = YES;
+    header.automaticallyChangeAlpha = YES;
     
     // 隐藏时间
     header.lastUpdatedTimeLabel.hidden = YES;
@@ -239,7 +239,7 @@ - (void)example19
 {
     // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadLastData方法)
     self.tableView.footer = [MJChiBaoZiFooter2 footerWithRefreshingTarget:self refreshingAction:@selector(loadLastData)];
-    self.tableView.footer.autoChangeAlpha = YES;
+    self.tableView.footer.automaticallyChangeAlpha = YES;
 }
 
 #pragma mark UITableView + 上拉刷新 自定义刷新控件(自动刷新)
@@ -333,6 +333,9 @@ - (NSMutableArray *)data
 {
     if (!_data) {
         self.data = [NSMutableArray array];
+        for (int i = 0; i<5; i++) {
+            [self.data addObject:MJRandomData];
+        }
     }
     return _data;
 }