iOS常用

IOS 下拉刷新的实现

2020-07-29  本文已影响0人  Fat_Blog

这里用到的是UIRefreshControl(这是系统自带的下拉刷新,使用非常方便)

效果图

Jul-29-2020 10-54-54.gif

代码实现

//系统自带的下拉刷新效果
    UIRefreshControl *control = [[UIRefreshControl alloc] init];
    [control addTarget:self action:@selector(reFreshstatus:) forControlEvents:UIControlEventValueChanged];
    //不用自定义frame
    [self.tableView addSubview:control];
    //一进来就有刷新效果(但刷新数据是要手动的)
    [control beginRefreshing];
    [self reFreshstatus:control];

下面介绍一下那个橙色条的实现(系统自带的下拉刷新只是会有一个刷新图标的显示)

-(void)shownewStatus:(int)count
{
    //首先将label控件隐藏在导航栏的后面
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height+self.navigationController.navigationBar.frame.size.height-35, [UIScreen mainScreen].bounds.size.width, 35);
    label.backgroundColor = [UIColor orangeColor];
    if(count == 0){
        label.text = @"没有最新的微博数据";
    }else{
        //count是传进来的数量
        label.text = [NSString stringWithFormat:@"已加载%d条最新的微博数据",count];
    }
    label.textColor = [UIColor whiteColor];
    //设置文字居中
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16.0];
    [self.navigationController.view insertSubview:label belowSubview:self.navigationController.navigationBar];
    //动画显示
    [UIView animateWithDuration:1.0 animations:^{
        label.transform = CGAffineTransformMakeTranslation(0, 35);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
            //一键回到最初的状态
            label.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            //不用的时候把label移除掉
            [label removeFromSuperview];
        }];
    }];
}
上一篇下一篇

猜你喜欢

热点阅读