iOS学习_动画
Posted 哦嘿嘿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS学习_动画相关的知识,希望对你有一定的参考价值。
UIView动画
改变UIView的frame:
- (IBAction)changeFrame:(id)sender { //UIView动画有开始beginAnimation, 有结束commitAnimation //第一步:开始UIview动画 [UIView beginAnimations:@"move" context:nil]; //第二步:设置动画的时长 [UIView setAnimationDuration:3]; //第三步:设置代理 [UIView setAnimationDelegate:self]; //第四步:设置相关的对象的frame self.testView.frame = CGRectMake(100, 100, 200, 100); //第五步:结束动画 [UIView commitAnimations]; } #pragma mark - UIViewAnimationDelegate的代理 //开始动画方法 - (void)animationWillStart:(NSString *)animationID context:(void *)context{ NSLog(@"ID = %@,context = %@",animationID,context); } //结束动画的方法 - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{ NSLog(@"ID = %@,context = %@",animationID,context); }
改变UIView的颜色
- (IBAction)changeColor:(id)sender { [UIView beginAnimations:@"color" context:nil]; [UIView setAnimationDuration:2]; [UIView setAnimationDelegate:self]; self.testView.backgroundColor = [UIColor redColor]; [UIView commitAnimations]; }
改变UIView的透明度
- (IBAction)changeAlpha:(id)sender { [UIView beginAnimations:@"alpha" context:nil]; [UIView setAnimationDuration:5]; [UIView setAnimationDelegate:self]; self.testView.alpha = 0.2; [UIView commitAnimations]; }
UIView的翻转效果
- (IBAction)rotaionAction:(id)sender { //开始动画 [UIView beginAnimations:@"rotation" context:nil]; //设置时长 [UIView setAnimationDuration:1.5f]; //设置淡入的效果 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //设置代理 [UIView setAnimationDelegate:self]; //设置翻转方向 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.testView cache:YES]; //结束 [UIView commitAnimations]; }
UIView的旋转效果
//旋转的效果 - (IBAction)transfromAction:(id)sender { [UIView beginAnimations:@"transfrom" context:nil]; [UIView setAnimationDuration:3.0f]; [UIView setAnimationDelegate:self]; [UIView setAnimationRepeatCount:-1]; //进行旋转,设置旋转角度 CGAffineTransform transfrom = CGAffineTransformMakeRotation(3*M_PI); //设置旋转的角度的对象 [self.testView setTransform:transfrom]; [UIView commitAnimations]; }
CoreAnimation动画
Layer的一些常用属性
//设置图片为圆角 self.imageview.layer.cornerRadius = self.imageview.frame.size.width/2; // //注意:还要设置(masksToBounds这个属性影响layer层的阴影效果) // self.imageview.layer.masksToBounds = YES; //设置layer的阴影颜色 self.imageview.layer.shadowColor = [UIColor redColor].CGColor; //设置透明度 self.imageview.layer.shadowOpacity = 0.1; //设置阴影的偏移量 self.imageview.layer.shadowOffset = CGSizeMake(20, 10); //设置阴影的模糊度 self.imageview.layer.shadowRadius = 1.0f; //需求:拖进来uiview,设置阴影, self.uiView.layer.shadowColor = [UIColor orangeColor].CGColor; self.uiView.layer.shadowOpacity = 0.5; self.uiView.layer.shadowOffset = CGSizeMake(30, 30); self.uiView.layer.shadowRadius = 2.0f;
自定义Layer
- (void)customLayout{ //创建layer对象 CALayer *layer = [CALayer layer]; //设置对象的位置 layer.frame = CGRectMake(0, 280, 100, 100); //设置背景颜色 layer.backgroundColor = [UIColor redColor].CGColor; //设置锚点 layer.anchorPoint = CGPointMake(0, 0); //设置锚点的大小(位置) layer.position = CGPointMake(50, 50); //layer需要添加到layer层 [self.view.layer addSublayer:layer]; }
CABasicAnimation动画
- (IBAction)basicAnimation:(id)sender { //第一步:创建动画的对象 CABasicAnimation *basicAnimation = [CABasicAnimation animation]; //第二步:告诉layer层,需要执行什么动画,后边设置的内容为CAlay的先关属性 basicAnimation.keyPath = @"position"; //第三步:告诉layer哪来,到哪去 basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)]; //注意:如果要实现移动到的位置不回到原来的位置需要实现以下两句代码; basicAnimation.removedOnCompletion = NO; //设置保存动画状态的内容 basicAnimation.fillMode = kCAFillModeForwards; //第四步;设置动画持续时长 basicAnimation.duration = 3.0f; //第五步:将要执行的动画添加到calayer上 [self.imageview.layer addAnimation:basicAnimation forKey:@"basic"]; }
CAKeyframeAnimation动画
- (IBAction)keyAction:(id)sender { //第一步:创建对象 CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation]; //第二步:设置动画轨迹 keyFrameAnimation.keyPath = @"transform.rotation"; //第三步:设置翻转角度(弧度的计算公式:度数 / 180 * M_PI) keyFrameAnimation.values = @[@(90/180.0*M_PI),@(180/180.0*M_PI),@(360/180.0*M_PI)]; //第四步;设置动画时长 keyFrameAnimation.duration = 3.0f; //第五步:将要执行的动画添加到layer上 [self.imageview.layer addAnimation:keyFrameAnimation forKey:@"keyFrameAnimation"]; }
CAAnimationGroupAction组动画
- (IBAction)CAAnimationGroupAction:(id)sender { //平移动画 CABasicAnimation *basicAnimation1 = [CABasicAnimation animation]; basicAnimation1.keyPath = @"transform.translation.y"; basicAnimation1.toValue = @(400); //翻转动画 CABasicAnimation *basicAnimation2 = [CABasicAnimation animation]; basicAnimation2.keyPath = @"transform.scale"; basicAnimation2.toValue = @(3);//弧度 //旋转动画 CABasicAnimation *basicAnimation3 = [CABasicAnimation animation]; basicAnimation3.keyPath = @"transform.rotation"; basicAnimation3.toValue = @(M_PI); //创建管理动画的动画组 CAAnimationGroup *group = [CAAnimationGroup animation]; group.animations = @[basicAnimation1,basicAnimation2,basicAnimation3]; group.duration = 3.0f; [self.imageview.layer addAnimation:group forKey:@"group"]; }
CASpring动画
- (IBAction)CASpringAction:(id)sender { //创建对象 CASpringAnimation *spring = [CASpringAnimation animation]; spring.keyPath = @"transform.scale"; spring.fromValue = @1; spring.toValue = @0.25; spring.duration = 2.0f; [self.imageview.layer addAnimation:spring forKey:@"spring"]; }
以上是关于iOS学习_动画的主要内容,如果未能解决你的问题,请参考以下文章