两个Loading动画和一个正方体

1.正方体

Core Animation定义了这个(消亡点)位于变换图层的anchorPoint ,当改变一个图层的position,你也改变了它的消亡点,做3D变换的时候要时刻记住这一点,当你视图通过调整m34来让它更加有3D效果,应该首先把它放置于屏幕中央,然后通过平移来把它移动到指定位置(而不是直接改变它的position),这样所有的3D图层都共享一个消亡点。

如果有多个视图或者图层,每个都做3D变换,那就需要分别设置相同的m34值,并且确保在变换之前都在屏幕中央共享同一个position,CALayer有一个属性叫做sublayerTransform。它也是CATransform3D类型,但和对一个图层的变换不同,它影响到所有的子图层。这意味着你可以一次性对包含这些图层的容器做变换,于是所有的子图层都自动继承了这个变换方法


_viewArray=[[NSMutableArray alloc]initWithCapacity:0];

_containerView=[[UIView alloc]init];

_containerView.backgroundColor=[UIColor grayColor];

_containerView.frame=CGRectMake(50, 50, 200, 200);

[self.view addSubview:_containerView];

for(int i=1;i<7;i++)

{

UIView *temp=[self createCustemView:i];

temp.center=CGPointMake(200/2, 200/2);

[_containerView insertSubview:temp atIndex:0];

[_viewArray insertObject:temp atIndex:0];

}

[self setTransform];

-(void)setTransform

{

CATransform3D perspective=CATransform3DIdentity;

perspective.m34=-1.0/500.0; //设置消亡点

_containerView.layer.sublayerTransform=perspective;//共用sublayerTransform

CATransform3D transform=CATransform3DMakeTranslation(0, 0, 50);//第六张view沿z移动50

[self getView:0 andTransform:transform];//6

transform=CATransform3DMakeTranslation(50, 0, 0);//第五张view沿x移动50 并且沿y旋转90度,注意这里旋转是按中点旋转主要看anchorPoint 一下以此类推

transform=CATransform3DRotate(transform, M_PI_2, 0, 1, 0);

[self getView:1 andTransform:transform];//5

transform=CATransform3DMakeTranslation(0, -50, 0);

transform=CATransform3DRotate(transform, M_PI_2, 1, 0, 0);

[self getView:2 andTransform:transform];//4

transform=CATransform3DMakeTranslation(0, 50, 0);

transform=CATransform3DRotate(transform, -M_PI_2, 1, 0, 0);

[self getView:3 andTransform:transform];//3

transform=CATransform3DMakeTranslation(-50, 0, 0);

transform=CATransform3DRotate(transform, -M_PI_2, 0, 1, 0);

[self getView:4 andTransform:transform];//2

transform=CATransform3DMakeTranslation(0, 0, -50);

transform=CATransform3DRotate(transform, M_PI, 0, 1, 0);

[self getView:5 andTransform:transform];//1

perspective=CATransform3DRotate(perspective, -M_PI_4, 1, 0, 0);//最后旋转下整体更好的看清楚(不旋转就是一个正面的立方体,只能看到一面)

//.layer.sublayerTransform=perspective;

perspective=CATransform3DRotate(perspective, -M_PI_4, 0, 1, 0);

_containerView.layer.sublayerTransform=perspective;

}

-(void)getView:(int)index andTransform:(CATransform3D)transform

{

UIView *temp=[_viewArray objectAtIndex:index];

temp.layer.transform=transform;

[self applyLightingToFace:temp.layer];

}

-(void)applyLightingToFace:(CALayer *)face //给每个面添加光亮和阴影,这一块代码不是很懂

{

CALayer *layer=[CALayer layer];

layer.frame=face.bounds;

[face addSublayer:layer];

CATransform3D transform=face.transform;

GLKMatrix4 matrix4=*(GLKMatrix4 *)&transform;//三矩阵转四矩阵

GLKMatrix3 matrix3=GLKMatrix4GetMatrix3(matrix4);

GLKVector3 normal=GLKVector3Make(0, 0, 1);

normal=GLKMatrix3MultiplyVector3(matrix3, normal);

normal=GLKVector3Normalize(normal);

GLKVector3 light=GLKVector3Normalize(GLKVector3Make(0, 1, -0.5));

float dotProduct=GLKVector3DotProduct(light, normal);

CGFloat shadow=1+dotProduct-0.5;

UIColor *color=[UIColor colorWithWhite:0 alpha:shadow];

layer.backgroundColor=color.CGColor;

}

-(UIView *)createCustemView:(int)number//创建正方体的各个面

{

UIView *view=[[UIView alloc]init];

view.backgroundColor=[UIColor whiteColor];

UILabel *label=[[UILabel alloc]init];

label.textAlignment=NSTextAlignmentCenter;

label.textColor=[UIColor redColor];

label.text=[NSString stringWithFormat:@"%d",number];

view.bounds=CGRectMake(0, 0, 100, 100);

label.frame=CGRectMake(0, (100-17)/2, 100, 17);

[view addSubview:label];

return view;

}

2.第一个loadding动画

一共是八个小圆圈排成一个大圆,第一个小圆在正上角


[self createAnimationLoadView];

-(void)createAnimationLoadView

{

_sequencePostionArray=[[NSMutableArray alloc]initWithCapacity:0];

_sequenceViewArray=[[NSMutableArray alloc]initWithCapacity:0];

NSArray *colorArray = @[[UIColor blueColor],[UIColor redColor],[UIColor purpleColor],[UIColor yellowColor],[UIColor blueColor],[UIColor blackColor],[UIColor grayColor],[UIColor orangeColor]];

//NSArray *colorArray=@[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor],[UIColor purpleColor],[UIColor blackColor],[UIColor orangeColor],[UIColor grayColor],[UIColor greenColor]];

CGFloat centerX=self.view.center.x;

CGFloat centerY=self.view.center.y;

CGFloat delta=2*M_PI /8;

CGFloat x = centerX + 50 * cos(delta*7);

CGFloat y = centerY + 50 * sin(delta*7);

[_sequencePostionArray addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];//第一个正小圆

view1.center = self.view.center;

view1.backgroundColor = colorArray[0];

view1.layer.cornerRadius = 10;

view1.layer.masksToBounds = YES;

view1.layer.transform = CATransform3DMakeScale(0, 0, 0);

[self.view addSubview:view1];

[_sequenceViewArray addObject:view1];

for(int i=0;i<7;i++)//绘制其余的7个小圆

{

CGFloat x = centerX + 50 * cos(delta*i);//

CGFloat y = centerY + 50 * sin(delta*i);

[_sequencePostionArray addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

view.center = self.view.center;

view.backgroundColor = colorArray[i+1];

view.layer.cornerRadius = 10;

view.layer.transform = CATransform3DMakeScale(0, 0, 0);

view.layer.masksToBounds = YES;

[self.view addSubview:view];

[_sequenceViewArray addObject:view];

}

-(void)startAnimation

{

for(int i=0;i<_sequenceViewArray.count;i++)//每个小圆都使用组动画

{

UIView *view=[_sequenceViewArray objectAtIndex:i];

[view.layer addAnimation:[self createGroupAnimationWithDelay:i*0.1 andIndex:i] forKey:@"group"];

}

}


-(CAAnimationGroup *)createGroupAnimationWithDelay:(CFTimeInterval)time andIndex:(NSInteger)index

{//分别改变属性的坐标和缩放大小

CABasicAnimation *moveAnimation=[CABasicAnimation animationWithKeyPath:@"position"];

moveAnimation.fromValue=[NSValue valueWithCGPoint:self.view.center];

moveAnimation.toValue=[_sequencePostionArray objectAtIndex:index];

moveAnimation.duration=0.75;

moveAnimation.autoreverses=YES;

CABasicAnimation *scaleAnimation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];

scaleAnimation.fromValue=@(0);

scaleAnimation.toValue=@(1);

scaleAnimation.duration=0.75;

scaleAnimation.autoreverses=YES;

CAAnimationGroup *group=[CAAnimationGroup animation];

group.animations=@[moveAnimation,scaleAnimation];

group.autoreverses=YES;

group.duration=0.75;

group.repeatCount=HUGE_VALF;

group.beginTime=CACurrentMediaTime()+time;

return group;

}

3第二个动画

旋转 并且有弹动的效果


-(CAKeyframeAnimation *)createRotateAnimation:(NSTimeInterval)time

{//方块从0度开始旋转60度,再下来旋转到90度,在旋转到110度(这里先转的角度都是与初始化的对比),在旋转到90度弹回,旋转到70度,旋转到90度... 

CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

animation.values=@[@(0),@(NUMBER_TO_MPI(60)),@(NUMBER_TO_MPI(90)),@(NUMBER_TO_MPI(110)),@(NUMBER_TO_MPI(90)),@(NUMBER_TO_MPI(70)),@(NUMBER_TO_MPI(90)),@(NUMBER_TO_MPI(100)),@(NUMBER_TO_MPI(90)),@(NUMBER_TO_MPI(80)),@(NUMBER_TO_MPI(90)),@(NUMBER_TO_MPI(95)),@(NUMBER_TO_MPI(90))];

animation.keyTimes=@[@(0),@(0.5),@(0.6),@(0.64),@(0.68),@(0.72),@(0.76),@(0.8),@(0.84),@(0.88),@(0.92),@(0.98),@(1)];// 旋转到各个角度的时间点,并不是区间的时间,一共才1s时间

animation.fillMode=kCAFillModeForwards;

animation.removedOnCompletion=NO;

animation.beginTime=CACurrentMediaTime()+time;

//animation.repeatCount=HUGE_VALF;

animation.duration=1;

return animation;

}

-(void)button1Click:(id)sender

{//每个view分别添加layer动画和移动中心点y,感觉是在向上翻动

[_yellowView.layer addAnimation:[self createRotateAnimation:0] forKey:@"yeallow"];

[_redView.layer addAnimation:[self createRotateAnimation:0.2] forKey:@"red"];

[_blueView.layer addAnimation:[self createRotateAnimation:0.4] forKey:@"blue"];

[UIView animateWithDuration:0.5 animations:^{

_yellowView.center = CGPointMake(_yellowView.center.x, _yellowView.center.y-10);

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.1 animations:^{

     _yellowView.center = CGPointMake(_yellowView.center.x, _yellowView.center.y+10); 

}];

}];

[UIView animateWithDuration:0.5 delay:0.2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{

      _redView.center = CGPointMake(_redView.center.x, _redView.center.y-10);

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.1 animations:^{

     _redView.center = CGPointMake(_redView.center.x, _redView.center.y+10);

}];

}];

[UIView animateWithDuration:0.5 delay:0.4 options:UIViewAnimationOptionBeginFromCurrentState animations:^{

        _blueView.center = CGPointMake(_blueView.center.x, _blueView.center.y-10); 

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.1 animations:^{

         _blueView.center = CGPointMake(_blueView.center.x, _blueView.center.y+10);

}];

}];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容