带有下拉刷新和锁定单元格的 UITableView,如 UltraVisual
UITableView with pull to refresh and locked cells like UltraVisual
我正在尝试做一些类似于 UltraVisual for iOS 已经做的事情。我想让我的下拉刷新位于其他单元格之间的单元格中。
我认为下面的 GIF 动画更好地解释了它:
看起来第一个单元格在向上拉时会淡出,而当您向下拉并位于 table 的顶部时,它会在第一个单元格的正下方添加一个新单元格并使用它作为下拉刷新。
有没有人做过类似的事情?
为 UV 写这篇文章。它实际上比您描述的要简单得多。此外,无论其价值如何,此视图被写为 UICollectionView
,但逻辑仍然适用于 UITableView
。
只有一个 header 单元格。在 'refresh' 动画期间,我简单地设置了 UICollectionView
的内容插入以使其保持打开状态。然后,当我完成重新加载后,我将内容插入设置回默认值。
至于弹性固定 header,有几种方法可以处理它。快速而肮脏的是使用UICollectionViewFlowLayout
,并修改- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
中的属性
这是一些伪代码,假设您的第一个单元格是粘性单元格 header:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *layoutAttributes = [super layoutAttributesForElementsInRect:rect];
if ([self contentOffsetIsBelowZero]) {
for (UICollectionViewLayoutAttributes *attributes in layoutAttributes) {
if (attributes.indexPath.item == 0) {
CGPoint bottomOrigin = CGPointMake(0, CGRectGetMaxY(attributes.frame));
CGPoint converted = [self.collectionView convertPoint:bottomOrigin toView:self.collectionView.superview];
height = MAX(height, CGRectGetHeight(attributes.frame));
CGFloat offset = CGRectGetHeight(attributes.frame) - height;
attributes.frame = CGRectOffset(CGRectSetHeight(attributes.frame, height), 0, offset);
break;
}
}
}
另一种方法是编写自定义 UICollectionViewLayout
并手动计算 CGRect's
。
最后,'fade out' 只不过是在第一个单元格移出屏幕时设置 objects 的不透明度。您可以在 - (void)applyLayoutAttributes…
期间计算单元格在屏幕上的位置,并据此设置不透明度。
最后,需要注意的事项:为了使用 UICollectionView
进行任何 'scroll based' 更新,您需要确保 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
returns YES
.您可以进行简单的优化检查,例如:
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
BOOL shouldInvalidate = [super shouldInvalidateLayoutForBoundsChange:newBounds];
if ([self contentOffsetIsBelowZero]) {
shouldInvalidate = YES;
}
return shouldInvalidate;
}
同样,这主要是伪代码,因此 re-write 基于您自己的实现。希望这对您有所帮助!
我正在尝试做一些类似于 UltraVisual for iOS 已经做的事情。我想让我的下拉刷新位于其他单元格之间的单元格中。
我认为下面的 GIF 动画更好地解释了它:
看起来第一个单元格在向上拉时会淡出,而当您向下拉并位于 table 的顶部时,它会在第一个单元格的正下方添加一个新单元格并使用它作为下拉刷新。
有没有人做过类似的事情?
为 UV 写这篇文章。它实际上比您描述的要简单得多。此外,无论其价值如何,此视图被写为 UICollectionView
,但逻辑仍然适用于 UITableView
。
只有一个 header 单元格。在 'refresh' 动画期间,我简单地设置了 UICollectionView
的内容插入以使其保持打开状态。然后,当我完成重新加载后,我将内容插入设置回默认值。
至于弹性固定 header,有几种方法可以处理它。快速而肮脏的是使用UICollectionViewFlowLayout
,并修改- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
这是一些伪代码,假设您的第一个单元格是粘性单元格 header:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *layoutAttributes = [super layoutAttributesForElementsInRect:rect];
if ([self contentOffsetIsBelowZero]) {
for (UICollectionViewLayoutAttributes *attributes in layoutAttributes) {
if (attributes.indexPath.item == 0) {
CGPoint bottomOrigin = CGPointMake(0, CGRectGetMaxY(attributes.frame));
CGPoint converted = [self.collectionView convertPoint:bottomOrigin toView:self.collectionView.superview];
height = MAX(height, CGRectGetHeight(attributes.frame));
CGFloat offset = CGRectGetHeight(attributes.frame) - height;
attributes.frame = CGRectOffset(CGRectSetHeight(attributes.frame, height), 0, offset);
break;
}
}
}
另一种方法是编写自定义 UICollectionViewLayout
并手动计算 CGRect's
。
最后,'fade out' 只不过是在第一个单元格移出屏幕时设置 objects 的不透明度。您可以在 - (void)applyLayoutAttributes…
期间计算单元格在屏幕上的位置,并据此设置不透明度。
最后,需要注意的事项:为了使用 UICollectionView
进行任何 'scroll based' 更新,您需要确保 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
returns YES
.您可以进行简单的优化检查,例如:
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
BOOL shouldInvalidate = [super shouldInvalidateLayoutForBoundsChange:newBounds];
if ([self contentOffsetIsBelowZero]) {
shouldInvalidate = YES;
}
return shouldInvalidate;
}
同样,这主要是伪代码,因此 re-write 基于您自己的实现。希望这对您有所帮助!