【发布时间】:2014-10-29 14:26:26
【问题描述】:
我想做Android Material Design Hierarchical Timing在iOS中使用UICollectionView引入的动画
假设它是一个collectionView,并且调整视图大小不是问题,及时执行此动画的最佳做法是什么。如何执行延迟
【问题讨论】:
标签: ios uicollectionview calayer material-design
我想做Android Material Design Hierarchical Timing在iOS中使用UICollectionView引入的动画
假设它是一个collectionView,并且调整视图大小不是问题,及时执行此动画的最佳做法是什么。如何执行延迟
【问题讨论】:
标签: ios uicollectionview calayer material-design
子类您的集合视图单元格并添加此方法:
class YourCollectionViewCell: UICollectionViewCell {
//Some IBOutlets if needed
func popAfter(delay: NSTimeInterval){
transform = CGAffineTransformMakeScale(0, 0)
UIView.animateWithDuration(0.7, delay: delay, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.transform = CGAffineTransformMakeScale(1, 1)
}, completion: nil)
}
}
将集合视图的delegate 和dataSource 设置为视图控制器(这可以在Interface Builder 中完成)。将常量添加到您的视图控制器并重新加载 viewDidAppear 中的集合视图以动画单元格:
class YourViewController{
private let numberOfCellsInLine = 3
private let numberOfVisibleCellsOnTheScreen = 12
private let baseDelay = 0.15
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
collectionView.reloadData()
}
}
为 UICollectionView 数据源和委托的视图控制器添加扩展:
extension YourViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return numberOfCells
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("YourCellID", forIndexPath: indexPath) as YourCollectionViewCell
//set cell's content
let index = indexPath.row
let yIndex = index / numberOfCellsInLine
let delay = Double(index % numberOfCellsInLine + (index >= numberOfVisibleCellsOnTheScreen ? 0 : yIndex)) * baseDelay
cell.popAfter(delay)
return cell
}
}
您可以在 Cell 的 popAfter 值中调整 baseDelay 和动画持续时间以实现所需的时间。希望这对您的应用有所帮助并祝您好运!欢迎随时提出任何问题。
【讨论】:
执行此操作的一种方法是使用计时器一次添加一个单元格,并让这些单元格在进入窗口时扩展为完整大小。
#import "ViewController.h"
#import "RDCollectionViewCell.h"
@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@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;
}
在自定义单元格类中,
@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];
}
项目可以在这里找到,http://jmp.sh/aDw846R
【讨论】:
这样的事情应该可以工作。我知道,随意拼凑的碎片。
Here's a very well done pen 你所描述的。在其中你会发现有问题的计时函数是曲线,在 CSS 中定义为:
cubic-bezier(0.650, 0.045, 0.405, 1.000)
知道了定时功能,就可以在iOS中实现这个动画了。这是另一个问题的链接——它展示了如何实现自定义动画,并且解释得很好。
Custom timing functions in iOS
可能需要一些简单的数学运算,但这应该会有所帮助! ...我希望。
干杯
【讨论】: