自定义 TableView 滚动指示器 (iOS)

Jos*_*nor 3 ios

我有一个使用表格视图的应用程序,有时此表格视图中可以有多达 100 个项目。因此,我被分配定制这个滚动指示器,以便用户可以轻松地滚动其内容。为此,我需要执行以下操作:

1)我需要滚动指示器始终显示。

2)我需要将滚动指示器从iOS默认的灰色指示器更改为橙色指示器,并在其中间添加一个向内延伸的标签。该标签将包含电池的日期。当您在滚动条中向下滚动时,日期会发生变化以反映您在页面上的位置。(参见图片以了解详情)。

3) 当您单击并按住此自定义 TableView 的滚动指示器时,它可以实现快速滚动。

解决这个问题的最佳方法是什么?我应该使用图书馆吗?

在此输入图像描述

Bra*_*n A 5

这不是一个完美的解决方案,但这就是我想出的,这应该会让您走上正确的道路,根据您的需求完善此解决方案。基本上,您需要执行以下几个基本步骤:

1实例化tableView(UITableView)和scrollViewIndicator(UIView)

2根据以下内容计算scrollIndicator的高度contentSize根据tableView的然后将tableView和scrollIndicator添加到容器视图中,并将滚动指示器添加到tableView上方,并将其alpha属性设置为0(以淡入我们滚动的方式)

3检查contentOffsettableView(UIScrollView的子类)的 并根据该值移动scrollIndicator

4一旦 tableView 减速,scrollIndicator 就会淡出

您的具体自定义滚动指示器将由您的项目和需求决定。这应该让您走上正确的轨道,但我认为您最大的问题是在引入“分页”后计算滚动指示器的高度。但我对你有信心!祝你好运,我的朋友。

#import "ViewController.h"

static CGFloat indicatorPadding =          5;
static CGFloat indicatorHeightMultiplyer = 0.05;
static CGFloat indicatorWidth =            3;
static CGFloat indicatorShowAnimation =    0.10;

@interface ViewController () <UITableViewDataSource, UITableViewDelegate> {
    CGFloat lastScrollOffset;
    BOOL    isFadingIndicator;
}

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView      *scrollIndicator;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    isFadingIndicator = NO;

    // Set Up TableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    self.tableView.rowHeight = 100;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView layoutIfNeeded];

    // Calculate indicator size based on TableView contentSize
    CGFloat indicatorHeight = self.tableView.contentSize.height * indicatorHeightMultiplyer;

    // Set Up Scroll Indicator
    self.scrollIndicator = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.tableView.frame) - indicatorPadding, indicatorPadding, indicatorWidth, indicatorHeight)];
    self.scrollIndicator.backgroundColor = [UIColor orangeColor];
    self.scrollIndicator.layer.cornerRadius = self.scrollIndicator.frame.size.width / 2;

    // Add TableView
    [self.view addSubview:self.tableView];

    // Add Scroll Indicator
    [self.view addSubview:self.scrollIndicator];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - TABLE VIEW METHODS

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

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

    if (scrollView == self.tableView) {

        [self showIndicator];

        if (lastScrollOffset >= scrollView.contentOffset.y) {
            [self moveScrollIndicatorDownward:YES withOffset:scrollView.contentOffset.y];
        }
        else {
            [self moveScrollIndicatorDownward:NO withOffset:scrollView.contentOffset.y]; // upward
        }

        lastScrollOffset = scrollView.contentOffset.y;

    }

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self hideIndicator];
}

#pragma mark - SCROLL INDICATOR METHODS

-(void)showIndicator {

    if (self.scrollIndicator.alpha == 0 && isFadingIndicator == NO) {
        // fade in scroll indicator
        isFadingIndicator = YES;
        [UIView animateWithDuration:indicatorShowAnimation animations:^{
            self.scrollIndicator.alpha = 1;
        } completion:^(BOOL finished) {
            isFadingIndicator = NO;
        }];

    }

}

-(void)hideIndicator {

    if (self.scrollIndicator.alpha == 1 && isFadingIndicator == NO) {
        // fade out scroll indicator
        isFadingIndicator = YES;
        [UIView animateWithDuration:indicatorShowAnimation animations:^{
            self.scrollIndicator.alpha = 0;
        } completion:^(BOOL finished) {
            isFadingIndicator = NO;
        }];
    }
}

-(void)moveScrollIndicatorDownward:(BOOL)downwards withOffset:(CGFloat)offset {

    if ([self canMoveScrollIndicator:downwards]) {

        self.scrollIndicator.center = CGPointMake(CGRectGetMidX(self.scrollIndicator.frame), (CGRectGetHeight(self.scrollIndicator.frame) / 2) + offset);

    }
    else {
        // maybe 'bounce' scroll indicator if isDecelerting is YES?
    }

}

-(BOOL)canMoveScrollIndicator:(BOOL)downwards {

    if (downwards) {
        if (self.scrollIndicator.frame.origin.y >= self.tableView.frame.size.height - indicatorPadding) {
            return NO;
        }
        else {
            return YES;
        }
    }
    else {
        // upwards
        if ((self.scrollIndicator.frame.origin.y + self.scrollIndicator.frame.size.height) <= self.tableView.frame.origin.y + indicatorPadding) {
            return NO;
        }
        else {
            return YES;
        }
    }

}

@end
Run Code Online (Sandbox Code Playgroud)