UI基础 - 训练营:使用CABasicAnimation实现视图绕X/Y轴旋转(3D效果)
视图 3D 旋转
1 - 以绕 X 轴旋转为例
1 #import "ViewController.h" 2 @interface ViewController () 3 @property(strong,nonatomic)UIImageView *logoIV; 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 self.view.backgroundColor = [UIColor whiteColor]; 12 13 // 视图 14 self.logoIV = [[UIImageView alloc] initWithFrame:CGRectMake(60, 200, self.view.frame.size.width - 120, self.view.frame.size.width - 120)]; 15 self.logoIV.backgroundColor = [UIColor yellowColor]; 16 [self.view addSubview:_logoIV]; 17 18 // 动画配置 19 CABasicAnimation *rotationAnimation; 20 // transform.rotation.y 环 Y 轴立体旋转 21 rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];// 绕 X 轴立体旋转 22 rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ]; 23 rotationAnimation.duration = 3; 24 rotationAnimation.cumulative = YES; 25 rotationAnimation.repeatCount = MAXFLOAT; 26 27 rotationAnimation.removedOnCompletion = NO; // 防止程序 前后台切换后动画静止 28 [self.logoIV.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 29 self.logoIV.layer.shadowOpacity = 1; // 阴影透明度 30 self.logoIV.layer.shadowColor = [UIColor blueColor].CGColor; // 阴影颜色 31 self.logoIV.layer.shadowRadius = 50; // 阴影半径 32 self.logoIV.layer.cornerRadius = (self.view.frame.size.width - 120)*0.5; 33 self.logoIV.layer.borderColor = [UIColor greenColor].CGColor; // 边框颜色 34 self.logoIV.layer.borderWidth = 20; // 边框半径 35 36 } 37 38 39 @end
运行效果