今天很匆忙的写了这篇文章,希望对大家与帮助。先展示一下动画效果!
都是一些简单的动画效果 // 缩放/*** 缩放*/- (void)scale { CALayer *scaleLayer = [[CALayer alloc] init]; scaleLayer.backgroundColor = [UIColor purpleColor].CGColor; scaleLayer.frame = CGRectMake(50, 70, 25, 25); scaleLayer.shadowOffset = CGSizeMake(0, 10); scaleLayer.shadowRadius = 3.0f; scaleLayer.shadowOpacity = 0.5f; scaleLayer.cornerRadius = scaleLayer.bounds.size.width * 0.5; scaleLayer.masksToBounds = YES; [self.view.layer addSublayer:scaleLayer]; CABasicAnimation *scaleAnimation = [[CABasicAnimation alloc] init]; scaleAnimation.keyPath = @"transform.scale"; // 设置动画的方式 scaleAnimation.fromValue = [NSNumber numberWithInteger:1]; // 起点 scaleAnimation.toValue = [NSNumber numberWithInteger:2]; // 终点 scaleAnimation.duration = 1.0f; // 设置动画的时间 scaleAnimation.repeatCount = MAXFLOAT; // 设置动画的次数 scaleAnimation.autoreverses = YES; // 返回原始状态是否动画方式 [scaleLayer addAnimation:scaleAnimation forKey:@"scale"]; // 添加动画}复制代码
// 平移
/** * 平移 */- (void)position { CALayer *positionLayer = [[CALayer alloc] init]; positionLayer.backgroundColor = [UIColor redColor].CGColor; positionLayer.cornerRadius = 5.0f; positionLayer.masksToBounds = YES; positionLayer.frame = CGRectMake(50, 140, 25, 25); [self.view.layer addSublayer:positionLayer]; CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; // 设置动画方式沿着X轴 positionAnimation.toValue = [NSNumber numberWithInteger:250]; positionAnimation.duration = 3.0f; positionAnimation.repeatCount = MAXFLOAT; positionAnimation.autoreverses = YES; [positionLayer addAnimation:positionAnimation forKey:@"position"];}复制代码
// 旋转
/** * 旋转 */- (void)rotate { CALayer *rotateLayer = [[CALayer alloc] init]; rotateLayer.backgroundColor = [UIColor blueColor].CGColor; rotateLayer.cornerRadius = 5.0f; rotateLayer.masksToBounds = YES; rotateLayer.frame = CGRectMake(50, 200, 50, 50); [self.view.layer addSublayer:rotateLayer]; CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2]; rotateAnimation.duration = 2.0f; rotateAnimation.autoreverses = NO; rotateAnimation.repeatCount = MAXFLOAT; [rotateLayer addAnimation:rotateAnimation forKey:@"rotate"];}复制代码
// 动画结合
/** * 动画组 */- (void)groupAnimation { CALayer *groupLayer = [[CALayer alloc] init]; groupLayer.backgroundColor = [UIColor grayColor].CGColor; groupLayer.frame = CGRectMake(50, 280, 25, 25); groupLayer.cornerRadius = 10.0f; groupLayer.masksToBounds = YES; [self.view.layer addSublayer:groupLayer]; // 缩放 CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0f]; scaleAnimation.toValue = [NSNumber numberWithFloat:2.0]; scaleAnimation.duration = 1.0f; scaleAnimation.repeatCount = MAXFLOAT; scaleAnimation.autoreverses = YES; // 平移 CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation"]; positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 400)]; positionAnimation.duration = 3.0f; positionAnimation.autoreverses = YES; // 旋转 CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2]; rotateAnimation.duration = 0.8f; rotateAnimation.autoreverses = NO; rotateAnimation.repeatCount = MAXFLOAT; // 动画组 CAAnimationGroup *animationGroup = [[CAAnimationGroup alloc] init]; animationGroup.duration = 2.0f; animationGroup.autoreverses = YES; animationGroup.repeatCount = MAXFLOAT; animationGroup.animations = @[scaleAnimation, positionAnimation, rotateAnimation]; [groupLayer addAnimation:animationGroup forKey:@"group"];}复制代码
//iOS还有一些其他的KeyPath来实现其他的动画
//transform.rotation.x 绕X轴旋转//transform.rotation.y 绕Y轴旋转//transform.scale.x 缩放X//transform.scale.y 缩放Y//transform.translation.x 平移X//transform.translation.y 平移Y复制代码