今天学习了一种加载UICollectionViewCell的动画效果,具体如图,代码也在下面:
这个效果是cell由小变大
//其实就是在这个方法里为cell添加了一个动画效果
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, 0, 0, 0, 1);//渐变
transform = CATransform3DTranslate(transform, 0, -100, 0);//左边水平移动
transform = CATransform3DScale(transform, 0, 0, 0);//由小变大
cell.layer.transform = transform;
cell.layer.opacity = 0.0;
[UIView animateWithDuration:0.6 animations:^{
cell.layer.transform = CATransform3DIdentity;
cell.layer.opacity = 1;
}];
}
这个效果是cell从左边水平移动进入
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, 0, 0, 0, 1);//渐变u
transform = CATransform3DTranslate(transform,-200, 0, 0);//左边水平移动
transform = CATransform3DScale(transform, 0, 0, 0);//由小变大
cell.layer.transform = transform;
cell.layer.opacity = 0.0;
[UIView animateWithDuration:0.6 animations:^{
cell.layer.transform = CATransform3DIdentity;
cell.layer.opacity = 1;
}];
}
还有一种动画效果
2.gif//动画代码如下
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
CATransform3D rotation;
rotation = CATransform3DMakeRotation((90.0 * M_PI) /180, 0.0, 0.7, 0.4);
rotation.m34 = 1.0/ -600;
cell.layer.shadowColor = [[UIColor blackColor]CGColor];
cell.layer.shadowOffset = CGSizeMake(10, 10);
cell.alpha = 0;
cell.layer.transform = rotation;
cell.layer.anchorPoint = CGPointMake(0, 0.5);
[UIView beginAnimations:@"rotation" context:NULL];
[UIView setAnimationDuration:0.8];
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1;
cell.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];
}
最后一种 先看下效果
3.gif这个实际上是对label设置的一个动画,不是针对于cell的,具体如下
在自定义的cell实现文件中
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, self.center.y - 15, 150, 50)];
[self addSubview:self.titleLabel];
}
return self;
}
- (void)show{
[UIView animateWithDuration:1 animations:^{
self.titleLabel.x = 30;
}];
}
- (void)hide{
[UIView animateWithDuration:1 animations:^{
self.titleLabel.x = 414;
}];
}
在控制器中的代码
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
AnimationCell *animationCell = (AnimationCell *)cell;
[animationCell show];
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
AnimationCell *animationCell = (AnimationCell *)cell;
[animationCell hide];
}
网友评论