【问题标题】:iCarousel Pull to Refresh and Load MoreiCarousel 拉动刷新并加载更多
【发布时间】:2018-10-13 16:06:24
【问题描述】:

我成功地集成了 iCarousel 组件,但现在在实现“拉动刷新”和“加载更多”功能时遇到了问题。

实际上 iCarousel 是 UIView 的子类,而“拉动刷新”和“加载更多”功能通常适用于 UIScrollView 的子类。 UIView 不支持这些功能。因此,我被困在这一点上。

我不知道如何使用 UIView(ICarousel) 实现“拉动刷新”和“加载更多”功能?

【问题讨论】:

    标签: ios objective-c swift uiscrollview icarousel


    【解决方案1】:

    解决方案

    您可以使用scrollOffset 属性和carouselDidScroll 方法来实现“拉动刷新”和“加载更多”功能。

    @property (nonatomic, assign) CGFloat scrollOffset;

    这是轮播的当前滚动偏移量,是 itemWidth 的倍数。这个值,四舍五入到最接近的整数,是 currentItemIndex 值。您可以使用此值在轮播移动时定位其他屏幕元素。如果您希望以编程方式将轮播滚动到特定偏移量,也可以设置该值。如果您希望禁用内置手势处理并提供您自己的实现,这可能会很有用。

    - (void)carouselDidScroll:(iCarousel *)carousel;

    每当滚动轮播时都会调用此方法。无论轮播是通过编程方式滚动还是通过用户交互滚动,都会调用它。

    这里有一些你需要知道的点。

    • scrollOffset < 0:用户正在尝试拉取刷新。

    • scrollOffset > numberOfItems - 2: 将显示最后一项

    carouselDidScroll 方法上实现此逻辑以存档功能。

    - (void)carouselDidScroll:(iCarousel *)carousel {
      // Start new pull request when user pulls |carousel| 
      // a distance equal to 0.4 width/height of an item
      if (carousel.scrollOffset < -0.4) {
        [self pullToRefresh];
      }
    
      // Start new load more request when last item will be displayed.
      // In this situation, I ignore cases when |numberOfItems| is small
      // Ex: |numberOfItems| < 2
      if (carousel.scrollOffset > carousel.numberOfItems - 2) {
        [self loadMore];
      }
    }
    
    - (void)pullToRefresh {
      // Make sure have only one request at a time
      if (self.isPullingToRefresh) {
        return;
      }
    
      self.isPullingToRefresh = YES;
    
      // Request API to receive new data
    
      // Update |isPullingToRefresh| when request finishes
      self.isPullingToRefresh = NO;
    }
    
    - (void)loadMore {
      // Make sure have only one request at a time
      if (self.isLoadingMore) {
        return;
      }
    
      self.isLoadingMore = YES;
    
      // Request API to receive new data
    
      // Update |isLoadingMore| when request finishes
      self.isLoadingMore = NO;
    }
    

    结果

    更多细节,你可以看看我的示例

    https://github.com/trungducc/stackoverflow/tree/icarousel-pull-to-refresh-load-more

    【讨论】:

    • 谢谢!!!这是一个非常好的答案,但我还想更新 Pull 的用户界面以刷新和加载更多功能。即: - 拉动刷新时集成UIRefreshControl - 加载更多时底部刷新控件
    • 在拉取刷新时集成 UIRefreshControl - 你不能这样做,因为 iCarousel 不是 UIScrollView。对于这两种情况下拉刷新和加载更多,您可以使用UIActivityIndicatorView 代替。只需将其添加到carousel.contentView,即可随时显示和隐藏。
    • 能否请您提供一些代码示例来描述隐藏和显示 UIActivityIndi​​catorView 的逻辑。
    • 示例项目是非常好的实现。但是你能不能让 pullToRefreshIndicator 在从 API 获取数据时保持在顶部。当 UIRefreshControl 与 UITableView 发生时发生
    • 当然可以。只需将 UIActivityIndi​​cator 添加到 _carousel.contentView 后更改其位置即可。您可以尝试在此行 github.com/nrober1409/iCarousel-PullToRefresh-LoadMore/blob/… 更改 75.0f 与另一个值,直到你得到你想要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    相关资源
    最近更新 更多