iOS-ios 上提加载更多怎么保持状态栏在底部的,求原理

WP主题讨论 WP主题讨论 主题:1013 回复:2239

iOS-ios 上提加载更多怎么保持状态栏在底部的,求原理

夜无邪 发布于 2017-01-11 字数 323 浏览 1085 回复 1

在做一个小应用的时候用到了“下拉刷新,上拉加载更多”的效果,但是怎么保持状态栏始终在tableview底部,(用了EGORefreshTableHeaderView插件),我知道需要更改图片的方向和laber的位置,但是laber怎么保持在底部呢?在网上有看到相关代码,但是没有原理,不是很理解,求大神指教原理。。最好带上代码吧,我是菜鸟》》》谢谢了

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?

评论(1

夜无邪 2017-07-12 1 楼

每个tableview有一个footerView,代表tableview的最下面的部分,用这个footerview来做上提加载更多,当没有下一页的时候隐藏这个footerview就可以了。

//
// LoadMoreTableFooterView.h
// KlipTao
//
// Created by Wang Dave on 11-11-13.
// Copyright (c) 2011年 DaveDev. All rights reserved.
//

import <UIKit/UIKit.h>

import <QuartzCore/QuartzCore.h>

typedef enum{
LoadMorePulling = 3,
LoadMoreNormal = 4,
LoadMoreLoading = 5,
} LoadMoreState;
@protocol LoadMoreTableFooterViewDelegate;
@interface LoadMoreTableFooterView : UIView
{

id _delegate;
LoadMoreState _state;

UILabel *_statusLabel;
CALayer *_arrowImage;
UIActivityIndicatorView *_activityView;

}

@property(nonatomic,assign) id <LoadMoreTableFooterViewDelegate> delegate;

(void)loadMoreScrollViewDidScroll:(UIScrollView )scrollView;
(void)loadMoreshScrollViewDidEndDragging:(UIScrollView
)scrollView;
(void)loadMoreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView;

@end
@protocol LoadMoreTableFooterViewDelegate

  • (void)loadMoreTableFooterDidTriggerRefresh:(LoadMoreTableFooterView*)view;
  • (BOOL)loadMoreTableFooterDataSourceIsLoading:(LoadMoreTableFooterView*)view;

@end

//
// LoadMoreTableFooterView.m
// KlipTao
//
// Created by Wang Dave on 11-11-13.
// Copyright (c) 2011年 DaveDev. All rights reserved.
//

define LoadMoreViewHight 60.0f

import "LoadMoreTableFooterView.h"

define TEXT_COLOR [UIColor darkGrayColor]

//[UIColor colorWithRed:87.0/255.0 green:108.0/255.0 blue:137.0/255.0 alpha:1.0]

define FLIP_ANIMATION_DURATION 0.18f

@interface LoadMoreTableFooterView (Private)

  • (void)setState:(LoadMoreState)aState;
    @end
    @implementation LoadMoreTableFooterView
    @synthesize delegate=_delegate;

  • (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {

      self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
      //self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];

// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, LoadMoreViewHight - 30.0f, self.frame.size.width, 20.0f)];
// label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// label.font = [UIFont systemFontOfSize:12.0f];
// label.textColor = TEXT_COLOR;
// label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
// label.shadowOffset = CGSizeMake(0.0f, 1.0f);
// label.backgroundColor = [UIColor clearColor];
// label.textAlignment = UITextAlignmentCenter;
// [self addSubview:label];
// _lastUpdatedLabel=label;
// [label release];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, LoadMoreViewHight - 48.0f, self.frame.size.width, 20.0f)];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    label.font = [UIFont boldSystemFontOfSize:13.0f];
    label.textColor = TEXT_COLOR;
    label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
    label.shadowOffset = CGSizeMake(0.0f, 1.0f);
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    [self addSubview:label];
    _statusLabel=label;
    [label release];

    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(25.0f, LoadMoreViewHight - LoadMoreViewHight, 30.0f, 55.0f);
    layer.contentsGravity = kCAGravityResizeAspect;
    layer.contents = (id)[UIImage imageNamed:@"blueArrow.png"].CGImage;

if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        layer.contentsScale = [[UIScreen mainScreen] scale];
    }

endif

    [[self layer] addSublayer:layer];
    _arrowImage=layer;

    UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    view.frame = CGRectMake(25.0f, LoadMoreViewHight - 38.0f, 20.0f, 20.0f);
    [self addSubview:view];
    _activityView = view;
    [view release];


    [self setState:LoadMoreNormal];

}

return self;

}

(void)setState:(LoadMoreState)aState{

switch (aState) {
case LoadMorePulling:

    _statusLabel.text = NSLocalizedString(@"松开即可加载更多...", @"松开即可加载更多...");
    [CATransaction begin];
    [CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
    _arrowImage.transform = CATransform3DMakeRotation((M_PI / 180.0) * 180.0f, 0.0f, 0.0f, 1.0f);
    [CATransaction commit];


    break;
case LoadMoreNormal:


    if (_state == LoadMorePulling) {
        [CATransaction begin];
        [CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
        _arrowImage.transform = CATransform3DIdentity;
        [CATransaction commit];
    }


    _statusLabel.text = NSLocalizedString(@"上拉即可加载更多...", @"上拉即可加载更多...");
    [_activityView stopAnimating];
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; 
    _arrowImage.hidden = NO;
    _arrowImage.transform = CATransform3DIdentity;
    [CATransaction commit];


    //[self refreshLastUpdatedDate];


    break;
case LoadMoreLoading:


    _statusLabel.text = NSLocalizedString(@"加载中...", @"加载中...");
    [_activityView startAnimating];
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; 
    _arrowImage.hidden = YES;
    [CATransaction commit];


    break;
default:
    break;

}

_state = aState;
}

pragma mark -

pragma mark ScrollView Methods

//手指屏幕上不断拖动调用此方法

  • (void)loadMoreScrollViewDidScroll:(UIScrollView *)scrollView {

    if (_state == LoadMoreLoading) {

      //CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);
      //offset = MIN(offset, 60);
      scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0f, LoadMoreViewHight, 0.0f);

    } else if (scrollView.isDragging) {

      BOOL _loading = NO;
      if ([_delegate respondsToSelector:@selector( loadMoreTableFooterDataSourceIsLoading:)]) {
          _loading = [_delegate loadMoreTableFooterDataSourceIsLoading:self];
      }
    
      if (_state == LoadMorePulling &amp;&amp; scrollView.contentOffset.y + (scrollView.frame.size.height) &lt; scrollView.contentSize.height + LoadMoreViewHight &amp;&amp; scrollView.contentOffset.y &gt; 0.0f &amp;&amp; !_loading) {
          [self setState:LoadMoreNormal];
      } else if (_state == LoadMoreNormal &amp;&amp; scrollView.contentOffset.y + (scrollView.frame.size.height) &gt; scrollView.contentSize.height + LoadMoreViewHight  &amp;&amp; !_loading) {
          [self setState:LoadMorePulling];
      }
    
      if (scrollView.contentInset.bottom != 0) {
          scrollView.contentInset = UIEdgeInsetsZero;
      }

    }

}

//当用户停止拖动,并且手指从屏幕中拿开的的时候调用此方法

  • (void)loadMoreshScrollViewDidEndDragging:(UIScrollView *)scrollView {

    BOOL _loading = NO;
    if ([_delegate respondsToSelector:@selector(loadMoreTableFooterDataSourceIsLoading:)]) {
    _loading = [_delegate loadMoreTableFooterDataSourceIsLoading:self];
    }

    if (scrollView.contentOffset.y + (scrollView.frame.size.height) > scrollView.contentSize.height + LoadMoreViewHight && !_loading) {

      if ([_delegate respondsToSelector:@selector(loadMoreTableFooterDidTriggerRefresh:)]) {
          [_delegate loadMoreTableFooterDidTriggerRefresh:self];
      }
    
      [self setState:LoadMoreLoading];
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.2];
      scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, LoadMoreViewHight, 0.0f);
      [UIView commitAnimations];

    }

}

//当开发者页面页面刷新完毕调用此方法,[delegate egoRefreshScrollViewDataSourceDidFinishedLoading: scrollView];

  • (void)loadMoreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.3];
    [scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
    [UIView commitAnimations];

    [self setState:LoadMoreNormal];

}

pragma mark -

pragma mark Dealloc

(void)dealloc {

_delegate=nil;
_activityView = nil;
_statusLabel = nil;
_arrowImage = nil;
//_lastUpdatedLabel = nil;
[super dealloc];
}

@end

怎么使用:
1.实现代理LoadMoreTableFooterViewDelegate
2.

 -(void)viewDidLoad{
...
if(self.tableView.tableFooterView == nil) {
[self loadMoreFooterInit];
}

}
-(void)loadMoreFooterInit{

if (_loadMoreTableFooterView == nil) {

    LoadMoreTableFooterView *view = [[LoadMoreTableFooterView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    view.delegate = self;
    self.tableView.tableFooterView = view;
    //[self.tableView addSubview:view];
    _loadMoreTableFooterView = view;
    [self.tableView.tableFooterView setHidden:YES];//self.tableView.tableFooterView 你的tableview
    [view release];
    //[_loadMoreTableFooterView setHidden:YES];

}

}