年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10878|回复: 1

ios最简单的方式实现下拉刷新功能

[复制链接]
  • TA的每日心情
    倒霉
    2020-11-28 20:31
  • 签到天数: 18 天

    [LV.4]偶尔看看III

    发表于 2014-8-27 12:50:50 | 显示全部楼层 |阅读模式
    1、下载第三方下拉刷新控件,推荐MJRefresh
    2、利用系统自带的UIRefreshControl:只适应于UITableViewControl,如果是UIViewControl上加的TableView则无法使用;
    3、重写UITableView,自定义下拉刷新功能,代码如下:
    1. //
    2. //  SABaseTableView.m
    3. //
    4. //  Created by Sian on 14-8-6.
    5. //  Copyright (c) 2014年 Sian. All rights reserved.
    6. //
    7. #import "SABaseTableView.h"
    8. @interface SABaseTableView ()
    9. {
    10.     UILabel                 *_headBase;
    11.     UILabel                 *_footBase;
    12.     UIActivityIndicatorView *_indicator;
    13.     CGFloat                 _space;
    14. }
    15. @end
    16. @implementation SABaseTableView
    17. #pragma mark - 初始化
    18. - (id)initWithFrame:(CGRect)frame
    19. {
    20.     self = [super initWithFrame:frame];
    21.     if (self) {
    22.         self.backgroundColor = kGrayColor(0.9);
    23.         _space = 60.0;
    24.         _headBase = [[UILabel alloc] init];
    25.         _headBase.backgroundColor = [UIColor clearColor];
    26.         _headBase.text = @"下拉可以刷新";
    27.         _headBase.textColor = kGrayColor(0.5);
    28.         _headBase.font = [UIFont boldSystemFontOfSize:14];
    29.         _headBase.textAlignment = NSTextAlignmentCenter;
    30.         [self addSubview:_headBase];
    31.         
    32.         _footBase = [[UILabel alloc] init];
    33.         _footBase.backgroundColor = [UIColor clearColor];
    34.         _footBase.text = @"上拉加载更多";
    35.         _footBase.textColor = kGrayColor(0.5);
    36.         _footBase.font = [UIFont boldSystemFontOfSize:14];
    37.         _footBase.textAlignment = NSTextAlignmentCenter;
    38.         [self addSubview:_footBase];
    39.         
    40.         _indicator = [[UIActivityIndicatorView alloc] init];
    41.         _indicator.center = CGPointMake(50, _space * 0.5);
    42.         _indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    43.         [_indicator stopAnimating];
    44.     }
    45.     return self;
    46. }
    47. #pragma mark 调整子控件
    48. - (void)layoutSubviews
    49. {
    50.     [super layoutSubviews];
    51.    
    52.     _headBase.frame = CGRectMake(0, -_space, self.frame.size.width, _space);
    53.     _footBase.frame = (CGRect){0, self.contentSize.height, _headBase.frame.size};
    54.     [self tableViewDraggingEven];
    55. }
    56. #pragma mark 监听TableView滑动区域
    57. - (void)tableViewDraggingEven
    58. {
    59.     CGFloat subtract = iOS7 ? (64 - self.superview.frame.origin.y) : 0;
    60.     CGFloat y = self.contentOffset.y + subtract;
    61.     CGFloat offsetMax = self.contentSize.height - self.frame.size.height;
    62.    
    63.     // ContentSize范围内
    64.     if (y > 0 && y < offsetMax)
    65.         return;
    66.    
    67.     // 触发刷新条件
    68.     if (!self.isDragging && self.refreshState == REFRESH_PULLING)
    69.         self.refreshState = REFRESH_REFRESHING;
    70.    
    71.     // 准备刷新范围
    72.     if ((y > -_space && y < 0) || (y > offsetMax && y < offsetMax + _space))
    73.         self.refreshState = REFRESH_NORMAL;
    74.    
    75.     // 即将刷新范围
    76.     if (y < -_space || y > offsetMax + _space + subtract)
    77.         self.refreshState = REFRESH_PULLING;
    78.    
    79.     self.refreshType = (y < 0) ? REFRESH_LATEST : REFRESH_MORE;
    80. }
    81. #pragma mark - 根据状态定义不同状态下的视图与动作
    82. - (void)setRefreshState:(REFRESH_STATE)refreshState
    83. {
    84.     CGFloat subtract = iOS7 ? (64 - self.superview.frame.origin.y) : 0;
    85.     REFRESH_TYPE refreshType = (self.contentOffset.y < 0) ? REFRESH_LATEST : REFRESH_MORE;
    86.     if (_refreshType == refreshType && _refreshState == REFRESH_REFRESHING)  return;
    87.     _refreshType = refreshType;
    88.    
    89.     if(_refreshState == refreshState) return;
    90.     _refreshState = refreshState;
    91.    
    92.     switch (refreshState) {
    93.             
    94.         // 正常状态
    95.         case REFRESH_NORMAL:
    96.             _headBase.text = @"下拉可以刷新";
    97.             _footBase.text = @"上拉加载更多";
    98.             break;
    99.             
    100.         // 即将刷新
    101.         case REFRESH_PULLING:
    102.             _headBase.text = @"释放即可刷新";
    103.             _footBase.text = @"释放加载更多";
    104.             break;
    105.             
    106.             
    107.         // 正在刷新
    108.         case REFRESH_REFRESHING:
    109.             _headBase.text = @"正在刷新中...";
    110.             _footBase.text = @"正在加载中...";
    111.             [self refreshWithType:refreshType];
    112.         {
    113.             CGFloat offsetMax = self.contentSize.height - self.frame.size.height;
    114.             UIEdgeInsets edgeInset = UIEdgeInsetsZero;
    115.             edgeInset.top = (self.refreshType == REFRESH_LATEST) ? _space + subtract : subtract;
    116.             edgeInset.bottom = (self.refreshType == REFRESH_MORE) ? _space: 0;
    117.             CGPoint offset = CGPointZero;
    118.             offset.y = (self.refreshType == REFRESH_LATEST) ? -_space - subtract: offsetMax + _space;
    119.             
    120.             [UIView animateWithDuration:0.3 animations:^{
    121.                 self.contentInset = edgeInset;
    122.                 self.contentOffset = offset;
    123.             }];
    124.         }
    125.             [_indicator startAnimating];
    126.             [(self.refreshType == REFRESH_LATEST) ? _headBase : _footBase addSubview:_indicator];
    127.             break;
    128.             
    129.             
    130.             
    131.         default:
    132.             break;
    133.     }
    134. }
    135. #pragma mark - 刷新时执行方法
    136. - (void)refreshWithType:(REFRESH_TYPE)refreshType
    137. {
    138.     switch (refreshType) {
    139.         case REFRESH_LATEST:
    140.             NSLog(@"下拉刷新...");
    141.             break;
    142.         case REFRESH_MORE:
    143.             NSLog(@"上拉加载更多...");
    144.             break;
    145.         default:
    146.             break;
    147.     }
    148. }
    149. #pragma mark 停止刷新
    150. - (void)endRefresh
    151. {
    152.     [UIView animateWithDuration:0.3 animations:^{
    153.         self.contentInset = UIEdgeInsetsZero;
    154.         [_indicator stopAnimating];
    155.         [_indicator removeFromSuperview];
    156.     }];
    157. }
    158. @end
    复制代码
    进入刷新状态会自动调用方法
    - (void)refreshWithType:(REFRESH_TYPE)refreshType
  • TA的每日心情
    倒霉
    2020-11-28 20:31
  • 签到天数: 18 天

    [LV.4]偶尔看看III

     楼主| 发表于 2014-8-27 12:52:20 | 显示全部楼层
    SABaseTableView.h
    1. #import <UIKit/UIKit.h>
    2. typedef enum{
    3.     REFRESH_NORMAL      = 0,    // 常规状态
    4.     REFRESH_PULLING     = 1,    // 准备刷新
    5.     REFRESH_REFRESHING  = 2,    // 正在刷新
    6. }REFRESH_STATE;
    7. @interface SABaseTableView : UITableView
    8. @property (nonatomic, assign)   REFRESH_TYPE    refreshType;
    9. @property (nonatomic, assign)   REFRESH_STATE   refreshState;
    10. // 正在刷新调用方法
    11. - (void)refreshWithType:(REFRESH_TYPE)refreshType;
    12. // 结束刷新
    13. - (void)endRefresh;
    14. @end
    复制代码
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-29 04:10 , Processed in 0.054780 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表