iCarousel 拉动刷新并加载更多
iCarousel Pull to Refresh and Load More
我能够成功集成 iCarousel 组件,但现在在实现 "Pull to Refresh" 和 "Load More" 功能时遇到问题。
实际上 iCarousel 是 UIView 的子类,而 "Pull to Refresh" & "Load More" 功能通常与 UIScrollView[=21= 的子类一起使用]. UIView 不支持这些功能。因此,我卡在了这一点上。
我不知道如何使用 UIView(ICarousel)?
实现 "Pull to Refresh" & "Load More" 功能
解决方案
您可以使用 scrollOffset
属性 和 carouselDidScroll
方法来实现 "Pull to Refresh" 和 "Load More" 功能。
@property (nonatomic, assign) CGFloat scrollOffset;
This is the current scroll offset of the carousel in multiples of the itemWidth. This value, rounded to the nearest integer, is the currentItemIndex value. You can use this value to position other screen elements while the carousel is in motion. The value can also be set if you wish to scroll the carousel to a particular offset programmatically. This may be useful if you wish to disable the built-in gesture handling and provide your own implementation.
- (void)carouselDidScroll:(iCarousel *)carousel;
This method is called whenever the carousel is scrolled. It is called regardless of whether the carousel was scrolled programmatically or through user interaction.
这里有一些你需要知道的要点。
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/Whosebug/tree/icarousel-pull-to-refresh-load-more
我能够成功集成 iCarousel 组件,但现在在实现 "Pull to Refresh" 和 "Load More" 功能时遇到问题。
实际上 iCarousel 是 UIView 的子类,而 "Pull to Refresh" & "Load More" 功能通常与 UIScrollView[=21= 的子类一起使用]. UIView 不支持这些功能。因此,我卡在了这一点上。
我不知道如何使用 UIView(ICarousel)?
实现 "Pull to Refresh" & "Load More" 功能解决方案
您可以使用 scrollOffset
属性 和 carouselDidScroll
方法来实现 "Pull to Refresh" 和 "Load More" 功能。
@property (nonatomic, assign) CGFloat scrollOffset;
This is the current scroll offset of the carousel in multiples of the itemWidth. This value, rounded to the nearest integer, is the currentItemIndex value. You can use this value to position other screen elements while the carousel is in motion. The value can also be set if you wish to scroll the carousel to a particular offset programmatically. This may be useful if you wish to disable the built-in gesture handling and provide your own implementation.
- (void)carouselDidScroll:(iCarousel *)carousel;
This method is called whenever the carousel is scrolled. It is called regardless of whether the carousel was scrolled programmatically or through user interaction.
这里有一些你需要知道的要点。
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/Whosebug/tree/icarousel-pull-to-refresh-load-more