一、UIView动画
//UIView动画有开始beginAnimation,有结束commitAnimation
//第一步:开始UIView动画
[UIView beginAnimations:@"标识ID(可随意命名)" context:nil];
//第二步:设置动画时长
[UIView setAnimationDuration:3];
//第三步:设置UIView动画的回调代理
[UIView setAnimationDelegate:self];
//第四步:设置相关的对象的frame(或者color,alpha等属性)
_testView.frame = CGRectMake(100, 100, 200, 100);
_testView.backgroundColor = [UIColor redColor];
_testView.alpha = 0.2;
//第五步:结束动画(提交动画效果)
[UIView commitAnimations];
第三步满足回调协议后走协议方法:
//开始动画时的方法
- (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);
}
二、CoreAnimation动画
1.layer的属性
#pragma mark - 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; //设置layer层的透明度 self.imageView.layer.shadowOpacity = 0.5f; //设置阴影的偏移量 self.imageView.layer.shadowOffset = CGSizeMake(-20, 10); //设置阴影的模糊度 self.imageView.layer.shadowRadius = 1.0f; //需求:拖进来一个UIView设置它的阴影 self.myView.layer.shadowColor = [UIColor blackColor].CGColor; self.myView.layer.shadowOpacity = 1; self.myView.layer.shadowOffset = CGSizeMake(20, 0); //self.imageView.layer.shadowRadius = 3; */
layer常用属性
2.各种动画实现方法步骤
- 声明方法名称
- 赋给对应动画操作的keyPath
- 设置动画操作需要的属性值
- 将该动画操作添加给图片的layer进行操作
#pragma mark - CASpringAnimation动画按钮的响应方法 - (IBAction)springAnimation:(id)sender { CASpringAnimation *springAnimation = [CASpringAnimation animation]; springAnimation.keyPath = @"transform.scale"; springAnimation.fromValue = @1; springAnimation.toValue = @0.25; springAnimation.duration = 3.0f; [self.imageView.layer addAnimation:springAnimation forKey:@"springAnimation"]; } #pragma mark - CAAnimationGroup组动画按钮的响应方法 - (IBAction)animationGroup:(id)sender { //平移动画 CABasicAnimation *basicAnimation1 = [CABasicAnimation animation]; basicAnimation1.keyPath = @"transform.translation.y"; basicAnimation1.toValue = @(400); //缩小动画 CABasicAnimation *basicAnimation2 = [CABasicAnimation animation]; basicAnimation2.keyPath = @"transform.scale"; basicAnimation2.toValue = @(0.2); //旋转动画 CABasicAnimation *basicAnimation3 = [CABasicAnimation animation]; basicAnimation3.keyPath = @"transform.rotation"; basicAnimation3.toValue = @(M_PI); //需要创建管理各个动画的动画组 CAAnimationGroup *group = [CAAnimationGroup animation]; group.animations = @[basicAnimation1, basicAnimation2, basicAnimation3]; group.duration = 5.0f; [self.imageView.layer addAnimation:group forKey:@"groupAnimation"]; } #pragma mark - CAKeyframeAnimation动画按钮的响应方法 - (IBAction)keyFrameAnimation:(id)sender { //第一步:创建对象 CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation]; //第二步:设置动画轨迹 keyFrameAnimation.keyPath = @"transform.rotation"; //第三步:设置旋转角度(弧度的计算公式:度数/180*π)(π = M_PI) keyFrameAnimation.values = @[@(0 / 180.0 * M_PI), @(360 / 180.0 * M_PI)]; keyFrameAnimation.repeatCount = 3; //第四步:设置时长 keyFrameAnimation.duration = 3.0f; //第五步:添加动画到layer层 [self.imageView.layer addAnimation:keyFrameAnimation forKey:@"keyFrameAnimation"]; } #pragma mark - CABasicAnimation动画的响应方法 - (IBAction)basicAnimation:(id)sender { /***************移动效果***************/ //第一步:创建动画对象 CABasicAnimation *basicAnimation = [CABasicAnimation animation]; //第二步:告诉layer层需要执行什么样子的动画[后面设置的内容为CALayer的相关属性] 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 = 4; //第五步:将要执行的动画添加到calayer上 [self.imageView.layer addAnimation:basicAnimation forKey:@"basic1"]; /***************翻转效果***************/ CABasicAnimation *basic = [CABasicAnimation animation]; basic.keyPath = @"transform"; //设置翻转到的地方 basic.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 0, 1)]; basic.duration = 2.0f; [self.imageView.layer addAnimation:basic forKey:@"basic2"]; //根据key去除移除动画 // [self.imageView.layer removeAnimationForKey:@"basic1"]; }