iOS一个小功能,实现一键返回顶部,按键上面显示列表内容个数和当前第几个内容

效果图 请看右下角的按钮

上代码,

创建控件

- (void)creatTapImageView {

    

    self.tapView = [[UIView alloc] initWithFrame:CGRectMake(KScreenW - 80, KScreenH - 80, 50, 50)];

    [self.view addSubview:_tapView];

    [self.view bringSubviewToFront:_tapView];

    _tapView.backgroundColor = [UIColor whiteColor];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoTopView:)];

    _tapView.hidden = YES;

    [_tapView addGestureRecognizer:tapGesture];

    _tapView.layer.masksToBounds = YES;

    _tapView.layer.cornerRadius = 25;

    _tapView.layer.borderWidth = 2;

    _tapView.layer.borderColor = YYColorFromRGB(0xd7d7d7).CGColor;

    

    self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 50, 20)];

    [self.tapView addSubview:_countLabel];

    _countLabel.textAlignment = NSTextAlignmentCenter;

    _countLabel.text = @"";

    _countLabel.font = Font(14);

    _countLabel.textColor = YYColorFromRGB(0x666666);

    

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(7, 25, 36, 1)];

    [_tapView addSubview:line];

    line.backgroundColor = YYColorFromRGB(0xd7d7d7);

    line.backgroundColor = [UIColor blackColor];

    self.numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 50, 20)];

    [self.tapView addSubview:_numLabel];

    _numLabel.textAlignment = NSTextAlignmentCenter;

    _numLabel.text = @"";

    _numLabel.font = Font(14);

    _numLabel.textColor = YYColorFromRGB(0x666666);

    

    self.tapImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    _tapImageView.image = [UIImage imageNamed:@"置顶"];

    [_tapView addSubview:_tapImageView];

    

}

点击回到顶部

- (void)gotoTopView:(UITapGestureRecognizer *)gesture {

    [self.chtableview setContentOffset:CGPointMake(0, 0) animated:YES];


}

我把他们写到这两个方法里是因为我的cell的高度不一样 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"将要展示1******%ld^^^^^^%ld  ",indexPath.section,_displayNum);

    

    if (indexPath.section > 2) {

        

    if (self.displayNum < indexPath.section) {

        self.countLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section + 1];

    }

    

    self.displayNum = indexPath.section;

    if (indexPath.section < 2) { //如果上滑到顶部,则吧endDisplayNum置零 否者会出现 1

        self.endDisplayNum = 0;

    }

    NSLog(@"将要展示2******%ld^^^^^^%ld  ",indexPath.section,_displayNum);

    }

}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

    

    NSLog(@"将要消失1******%ld^^^^%ld",indexPath.section,_endDisplayNum);

    if (indexPath.section > 2) {

        

    if (self.endDisplayNum > indexPath.section) {

        self.countLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section + 1];

    }

    

    self.endDisplayNum = indexPath.section;

    NSLog(@"将要消失2******%ld^^^^%ld",indexPath.section,_endDisplayNum);

    }

}

//开始拖动

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    

    self.tapImageView.hidden = YES;

    

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    

    self.tapImageView.hidden = NO;

    

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    

    if (scrollView.contentOffset.y > 200) {

        self.tapView.hidden = NO;

    }else{

        self.tapView.hidden = YES;

    }

    

}



猜你喜欢

转载自blog.csdn.net/xiaoqi307/article/details/80164408