2015-03-13 58 views
1

我想做的東西類似於UltraVisual iOS已經做了什麼。我想讓我的刷新功能位於其他單元格之間的單元格中。UITableView與拉刷新和鎖定的單元格像UltraVisual

我認爲下面的GIF動畫更好地解釋它:

UltraVisual pull-to-refresh

它看起來拉起時,像第一個單元格淡出,當你拉下來,而和你在表的頂部,它會在第一個單元的下面添加一個新單元,並將其用作拉到刷新。

有沒有人做過類似的事情?

回答

1

寫了這個用於UV。其實際上比你所描述的更簡單。此外,對於它的價值,這個觀點被寫爲UICollectionView,但該邏輯仍然適用於UITableView

只有一個標題單元格。在刷新「刷新」動畫時,我只是簡單地設置了UICollectionView的內容,以使其保持打開狀態。然後,當我完成重新加載時,我會將內容插入到默認值。

至於彈性的固定標題,有幾種方法可以處理它。快速和骯髒的是使用UICollectionViewFlowLayout,並在- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

修改屬性下面是一些僞代碼,假設你的第一個單元格的粘頭:

- (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

最後,「淡出」實際上只不過是將第一個單元格內的對象的不透明度設置爲離開屏幕而已。您可以在- (void)applyLayoutAttributes…期間計算屏幕上單元格的位置,並基於此設置不透明度。

最後,需要注意的是:爲了使用UICollectionView進行任何'基於卷軸的'更新,您需要確保- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds返回YES。你可以做一個簡單的優化檢查,如:

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { 
    BOOL shouldInvalidate = [super shouldInvalidateLayoutForBoundsChange:newBounds]; 

    if ([self contentOffsetIsBelowZero]) { 
     shouldInvalidate = YES; 
    } 

    return shouldInvalidate; 
} 

再次,這是大多是僞代碼,所以重新根據自己的實現寫。希望這可以幫助!