CollectionView動畫

20150717102107315.jpg

廢話不說,先上效果圖,如果覺得效果不錯的話,可以繼續往下看。

DynamicCell.gif

這是我在cocoaChina上看到的,不過那位仁兄用的xib完成的,但我發現如果用純代碼寫的話,按照他的的那個思路并不能實現這個效果。

整個視圖用的是collectionView的(當然你也可以用UITableView來寫)下面的代碼只是簡單的創建UICollectionView。


UICollectionViewFlowLayout*flowLayout = [UICollectionViewFlowLayoutnew];

[flowLayoutsetScrollDirection:UICollectionViewScrollDirectionVertical];

self.collectionView= [[UICollectionViewalloc]initWithFrame:CGRectMake(0,64,self.view.width,self.view.height)collectionViewLayout:flowLayout];

self.collectionView.contentInset=UIEdgeInsetsMake(0,0,60,0);

self.collectionView.delegate=self;

self.collectionView.dataSource=self;

self.collectionView.backgroundColor= [UIColorcolorWithRed:0.369green:0.357blue:0.604alpha:1.000];

[self.collectionViewregisterClass:[CustomCollectionCellclass]forCellWithReuseIdentifier:@"CollectionViewCell"];

[self.viewaddSubview:self.collectionView];

[self.collectionViewselectItemAtIndexPath:[NSIndexPathindexPathForItem:0inSection:0]animated:YESscrollPosition:UICollectionViewScrollPositionBottom];

self.automaticallyAdjustsScrollViewInsets=NO;

緊接著需要實現collectionView的代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath 

其實這個動態效果就只是一個frame的變化,在生成cell的時候讓它的frame為

- (void)prepareVisibleCellsForAnimationWithRow:(NSInteger )row
                                       andCell:(CustomCollectionCell *)cell{

    cell.frame = CGRectMake(-CGRectGetWidth(cell.bounds),
                            cell.frame.origin.y,
                            CGRectGetWidth(cell.bounds),
                            CGRectGetHeight(cell.bounds));
    cell.alpha = 0.f;
}

這樣只是讓它整個的cell在屏幕的左邊,之后可以用一個animation改變frame,成一個動態的效果。

- (void)animateVisibleCell:(CustomCollectionCell *)cell withRow:(NSInteger)row {

    cell.alpha = 1.f;
    [UIView animateWithDuration:0.25 delay:0.2 *row usingSpringWithDamping:0.8
          initialSpringVelocity:1.0 options:0 animations:^{
        cell.frame = CGRectMake(0.f, cell.frame.origin.y,
                                CGRectGetWidth(cell.bounds),
                                CGRectGetHeight(cell.bounds));
    } completion:^(BOOL finished) {
        
    }];
}

這兩個方法在生成cell的地方調用即可,至于那個cell你也可以自定義一個。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    NSDictionary *dictionary = self.dataArray[indexPath.row];
    cell.model = dictionary;
    if (collectionView.contentSize.height - kScreen_Height > collectionView.contentOffset.y ||
        collectionView.contentOffset.y < 0) {
        if (collectionView.contentOffset.y == 0) {
            [self prepareVisibleCellsForAnimationWithRow:indexPath.row andCell:cell];
            [self animateVisibleCell:cell withRow:indexPath.row];
        }else {
            [self prepareVisibleCellsForAnimationWithRow:1 andCell:cell];
            [self animateVisibleCell:cell withRow:1];
        }
    }
    return cell;
}

if 中的判斷只是在頂部下拉和在底部上拉的時候不會調用方法,以及在剛生成或剛進入該視圖的時候(這時候collectionView.contentOffset.y == 0)屏幕中cell的生成會有一個延遲。
這樣就實現上面的那個動態效果。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,246評論 4 61
  • 翻譯自“Collection View Programming Guide for iOS” 0 關于iOS集合視...
    lakerszhy閱讀 3,925評論 1 22
  • 三年前寫的故事翻出來補一張封面。
    夏和夏和閱讀 459評論 4 6
  • 不必等我啦,我知道等待的滋味,有的人適合做一灣溪流,任由時間的流逝,那等待的初衷要無半點偏移,那過程里的孤獨寂寞要...
    李虛懷閱讀 457評論 0 0
  • 1.吸塵器 2.插座轉換器 3.便攜購物車 4.真空壓縮袋把你的巨大棉被變成小不點 5.廚房瀝水架 6.瀝水刀架 ...
    HillmanYang閱讀 860評論 0 1