菜鸟教程小白 发表于 2022-12-12 11:40:36

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


                                            <p><p>我有一个使用 tableview 的应用程序,有时这个 tableview 中可以有多达 100 个项目。因此,我被指派自定义此滚动指示器,以便用户轻松滚动浏览其内容。为此,我需要实现以下内容:</p>

<p>1) 我需要滚动指示器始终显示。</p>

<p>2) 我需要将滚动指示器从 iOS 默认的灰色指示器更改为橙色指示器,并在其中间添加一个向内延伸的标签。此标签将包含单元格的日期。当您在滚动条中向下滚动时,日期会更改以反射(reflect)您在页面上的位置。 (请参见图片进行说明)。</p>

<p>3) 当您单击并按住此自定义 TableView 的滚动指示器时,它会启用快速滚动。 </p>

<p>解决此问题的最佳方法是什么?我应该使用图书馆吗?</p>

<p> <a href="/image/Mj9Qe.png" rel="noreferrer noopener nofollow"><img src="/image/Mj9Qe.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这不是一个完美的解决方案,但这是我想出的,这应该让您走上正确的轨道,以完善此解决方案以满足您的需求。基本上你需要做几个基本的步骤:</p>

<p><strong>1</strong>实例化一个tableView(UITableView)和scrollViewIndicator(UIView)</p>

<p><strong>2</strong> 根据tableView的<code>contentSize</code>计算scrollIndicator的高度。然后将 tableView 和 scrollIndicator 添加到容器 View 中,并将 tableView 上方的滚动指示器及其 alpha 属性设置为 0(我们滚动时淡入)</p>

<p><strong>3</strong>检查tableView(UIScrollView的子类)的<code>contentOffset</code>,并根据该值移动scrollIndicator</p>

<p><strong>4</strong>一旦 tableView 减速,就将 scrollIndicator 淡出</p>

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

<pre><code>#import &#34;ViewController.h&#34;

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

@interface ViewController () &lt;UITableViewDataSource, UITableViewDelegate&gt; {
    CGFloat lastScrollOffset;
    BOOL    isFadingIndicator;
}

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

@end

@implementation ViewController

- (void)viewDidLoad {
    ;

    isFadingIndicator = NO;

    // Set Up TableView
    self.tableView = [ initWithFrame:self.view.bounds];
    forCellReuseIdentifier:@&#34;Cell&#34;];
    self.tableView.rowHeight = 100;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    ;

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

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

    // Add TableView
    ;

    // Add Scroll Indicator
    ;
}

- (void)didReceiveMemoryWarning {
    ;

}

#pragma mark - TABLE VIEW METHODS

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

    UITableViewCell *cell = ;

    return cell;
}

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

    return 10;
}

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

    if (scrollView == self.tableView) {

      ;

      if (lastScrollOffset &gt;= scrollView.contentOffset.y) {
            ;
      }
      else {
            ; // upward
      }

      lastScrollOffset = scrollView.contentOffset.y;

    }

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    ;
}

#pragma mark - SCROLL INDICATOR METHODS

-(void)showIndicator {

    if (self.scrollIndicator.alpha == 0 &amp;&amp; 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 &amp;&amp; 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.scrollIndicator.center = CGPointMake(CGRectGetMidX(self.scrollIndicator.frame), (CGRectGetHeight(self.scrollIndicator.frame) / 2) + offset);

    }
    else {
      // maybe &#39;bounce&#39; scroll indicator if isDecelerting is YES?
    }

}

-(BOOL)canMoveScrollIndicator:(BOOL)downwards {

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

}

@end
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 自定义 TableView 滚动指示器 (iOS),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37886757/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37886757/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 自定义 TableView 滚动指示器 (iOS)