iOS Android Material Design Hierarchical Timing using UICollectionView

后端 未结 3 1428
不知归路
不知归路 2021-02-08 06:20

I want to do the animation introduced by Android Material Design Hierarchical Timing in iOS using UICollectionView

Lets say its a collectionView, and resizing the view i

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-08 06:43

    One way to do this would be to add the cells one at a time with a timer, and have those cells expand to full size as they come on to the window.

    #import "ViewController.h"
    #import "RDCollectionViewCell.h"
    
    @interface ViewController () 
    @property (weak,nonatomic) IBOutlet UICollectionView *collectionview;
    @property (strong,nonatomic) NSMutableArray *mutableArray;
    @property (strong,nonatomic) NSArray *data;
    @end
    
    @implementation ViewController
    
    -(void)viewDidLoad {
        [super viewDidLoad];
        self.mutableArray = [NSMutableArray new];
        self.data = @[@"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", @"nine", @"ten"];
        [self performSelector:@selector(startTimer) withObject:nil afterDelay:0.5];
    }
    
    -(void)startTimer {
        [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(addCells:) userInfo:nil repeats:YES];
    }
    
    
    -(void)addCells:(NSTimer *) timer {
        static int counter = 0;
        [self.mutableArray addObject:self.data[counter]];
        counter ++;
        [self.collectionview insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.mutableArray.count -1 inSection:0]]];
        if (self.mutableArray.count == self.data.count) {
            [timer invalidate];
        }
    }
    
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return self.mutableArray.count;
    }
    
    
    -(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        RDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = (indexPath.row % 2 == 0)? [UIColor colorWithRed:180/255.0 green:210/255.0 blue:254/255.0 alpha:1] : [UIColor colorWithRed:50/255.0 green:167/255.0 blue:85/255.0 alpha:1];
        cell.label.text = self.mutableArray[indexPath.row];
        return cell;
    }
    

    In the custom cell class,

    @implementation RDCollectionViewCell
    
    -(void)awakeFromNib {
        self.contentView.transform = CGAffineTransformMakeScale(0.01, 0.01);
    }
    
    
    -(void)didMoveToWindow {
        [UIView animateWithDuration:0.3 delay:0.1 options:0 animations:^{
            self.contentView.transform = CGAffineTransformIdentity;
        } completion: nil];
    }
    

    The project can be found here, http://jmp.sh/aDw846R

提交回复
热议问题