ios 上拉载入下拉刷新Dome

为练手写了一个小的上拉载入很多其它下拉刷新的小的Dome 。

没有太多的技术含量,仅仅是作为新手的启示用。是上一篇下拉载入的扩展。先看一下那个再看这个就easy非常多。


Dome下载:http://download.****.net/detail/u010123208/8062715


先来梳理一下:

上拉家在很多其它就是上拉之后在底部现实一个视图,用来提示用户上拉载入很多其它,假设用户上拉就出发事件。进行载入并在试图中给予提示,同一时候后台载入数据,将加入的数据加入数据数组,最后又一次导入列表;

下拉刷新相同的操作,仅仅只是就是将数组清空又一次来导入数据;

首先我们要创建两个view 一个是顶部显示,一个在底部显示 ;

在view中要显示如今文字,一张图片,一个活动指示框(当进行网络请求载入数据的时候显示。其它时候隐藏),假设不太理解下载Dome看一下就知道了。

typedef enum {
    InsertStateNomal,    //寻常状态
    InsertStateDrapUp,   //上拉状态
    InsertStateDrapDown, //下拉状态
    InsertStateRun,      //正在载入的状态
}InsertState;

@interface HeadView : UIView

@property (nonatomic,strong) UIImageView *imageView; 
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,assign) InsertState insertState;
@property (nonatomic,strong) UIActivityIndicatorView *activity;

- (void)setInsertNomal;  
- (void)setInsertDrapDown;
- (void)setInsertRun;
@end


@interface FootView : UIView

@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,assign) InsertState insertState;
@property (nonatomic,strong) UIActivityIndicatorView *activity;

- (void)setInsertNomal;
- (void)setInsertDrapDoUp;
- (void)setInsertRun;

@end

枚举用来指示视图的当前状态;


事实上两个视图的内容什么的都全然一样;

//
//  InsertView.m
//  RefreshDome
//
//  Created by 小屁孩 on 14-10-16.
//  Copyright (c) 2014年 XS. All rights reserved.
//

#import "InsertView.h"

@implementation HeadView

@synthesize imageView;
@synthesize label;
@synthesize insertState;
@synthesize activity;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor orangeColor];
        //图片
        imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow.png"]];
        imageView.frame = CGRectMake(20, frame.size.height-60, 30, 50);
        [self addSubview:imageView];
        
        //显示的文字
        label = [[UILabel alloc]initWithFrame:CGRectMake(60, frame.size.height-60, 250, 50)];
        label.text = @"下拉刷新...";
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        
        //状态
        insertState = InsertStateNomal;
        
        //活动指示器
        activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activity.frame = CGRectMake(20,frame.size.height-60, 30, 50);
        [self addSubview:activity];
    }
    return self;
}

//初始状态
-(void)setInsertNomal
{
    insertState = InsertStateNomal;
    label.text = @"下拉刷新.....";
    imageView.layer.transform = CATransform3DMakeRotation(M_PI*2, 0, 0, 1);
    [activity stopAnimating];
    imageView.hidden =NO;
}

//下拉状态
- (void)setInsertDrapDown
{
    insertState = InsertStateDrapDown;
    [UIView beginAnimations:nil context:nil];
    label.text = @"释放后刷新.....";
    imageView.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
    [UIView commitAnimations];
    
}

//刷新状态
- (void)setInsertRun
{
    insertState =InsertStateRun;
    label.text = @"正在刷新.....";
    imageView.hidden = YES;
    [activity startAnimating];
}
@end


@implementation FootView

@synthesize imageView;
@synthesize label;
@synthesize insertState;
@synthesize activity;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor orangeColor];
        //图片
        imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow.png"]];
        imageView.layer.transform = CATransform3DMakeRotation(M_PI , 0, 0, 1);
        imageView.frame = CGRectMake(20, 10, 30, 50);
        [self addSubview:imageView];
        
        //文字
        label = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 250, 50)];
        label.text = @"上拉载入很多其它.....";
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        
        //状态
        insertState = InsertStateNomal;
        
        //活动指示器
        activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activity.frame = CGRectMake(20, 10, 30, 50);
        [self addSubview:activity];
    }
    return self;
}

//初始状态
- (void)setInsertNomal
{
    insertState = InsertStateNomal;
    label.text = @"上拉载入很多其它.....";
    imageView.layer.transform = CATransform3DMakeRotation(M_PI , 0, 0, 1);
    [activity stopAnimating];
    imageView.hidden =NO;
}

//上拉状态
- (void)setInsertDrapDoUp
{
    insertState = InsertStateDrapUp;
    [UIView beginAnimations:nil context:nil];
    label.text = @"释放后载入很多其它.....";
    imageView.layer.transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
    [UIView commitAnimations];
}

//载入状态
- (void)setInsertRun
{
    insertState = InsertStateRun;
    label.text = @"正在载入.....";
    imageView.hidden = YES;
    [activity startAnimating];
}
@end


最后就是列表了;


事实上在这里面须要注意的:

  • 要如何为表格加入视图
  • 加入的底部视图随着内容的增多,是不断变化的(这里我用了KVO来控制,当数组元素变化后处理位置)
  • 当完毕下拉或者上拉的时候。短暂的视图显示怎么来控制
  • 上拉下拉的推断(UIScrollview代理)条件;

#import <UIKit/UIKit.h>
#import "InsertView.h"

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *table;
@property (nonatomic,strong) NSMutableArray *tableArray;


@end