iCarousel拉动刷新并加载更多

Asi*_*lal 5 objective-c uiscrollview ios icarousel swift

我成功地能够集成iCarousel组件,但现在面临一个问题,即实现"Pull to Refresh"和"Load More"功能.

实际上iCarousel是UIView的子类,而"Pull to Refresh"和"Load More"功能通常适用于UIScrollView的子类.和UIView的不支持这些功能.因此,我陷入了困境.

我不知道如何用UIView(ICarousel)实现"Pull to Refresh"和"Load More"功能?

tru*_*duc 6

您可以使用scrollOffset属性和carouselDidScroll方法来实现"Pull to Refresh"和"Load More"功能.

@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;
}
Run Code Online (Sandbox Code Playgroud)

结果

有关更多详细信息,您可以查看我的示例

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