加一个延着y轴翻转的动画

给一个imageview加上一个延着y周旋转的动画(如抢红包),那么这个imageview不能直接加在vc.view上。

这是我做的一个Demo,红色方块与蓝色矩形的super view都是self.view,当红色方块做绕Y轴旋转动画时,只看到一半的红色方形在旋转。

- (void)viewDidLoad {  

[superviewDidLoad];// Do any additional setup after loading the view, typically from a nib.//

image view_pmainPicture=[[UIImageViewalloc]initWithFrame:CGRectMake(0,100, [[UIScreen mainScreen] bounds].size.width,190.0f)];    _pmainPicture.backgroundColor= [UIColorblueColor];    _pmainPicture.contentMode= UIViewContentModeScaleAspectFill;    _pmainPicture.userInteractionEnabled=YES;    [self.viewaddSubview:_pmainPicture];

//button_pButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

  [_pButton setTitle: @"请点击"forState: UIControlStateNormal];

  _pButton.frame= CGRectMake(_pSwitch.frame.origin.x, _pSwitch.frame.origin.y+100,200,44);

  _pButton.backgroundColor= [UIColorblueColor];  

[_pButton setTitleColor: [UIColorredColor] forState: UIControlStateNormal];    [_pButton addTarget:selfaction:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 

  _pButton.accessibilityIdentifier= @"myButton"; 

  [self.viewaddSubview: _pButton];// 头像_headImageView = [[UIImageViewalloc] init];

  _headImageView.frame= CGRectMake(0,100,100,100);    _headImageView.backgroundColor= [UIColorredColor];    [self.viewaddSubview:_headImageView];}


这是按钮点击方法

-(void) buttonAction:(UIButton*) button{    [selfrotate360WithDuration:2.0repeatCount:1];    _headImageView.backgroundColor= [UIColorredColor];    _headImageView.animationDuration=2.0;    _headImageView.animationRepeatCount=1;

  [_headImageView startAnimating];}


这是绕Y轴旋转的动画

- (void)rotate360WithDuration:(CGFloat)aDuration repeatCount:(CGFloat)aRepeatCount{

  CAKeyframeAnimation *theAnimation = [CAKeyframeAnimation animation];    theAnimation.values= [NSArrayarrayWithObjects:      

                  [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0,0,1,0)],                          [NSValue valueWithCATransform3D:CATransform3DMakeRotation(3.13,0,1,0)],                          [NSValue valueWithCATransform3D:CATransform3DMakeRotation(3.13,0,1,0)],                          [NSValue valueWithCATransform3D:CATransform3DMakeRotation(6.26,0,1,0)],nil];    theAnimation.cumulative=YES; 

  theAnimation.duration= aDuration;   

theAnimation.repeatCount= aRepeatCount;    theAnimation.removedOnCompletion=YES;    [_headImageView.layeraddAnimation:theAnimation forKey:@"transform"];}


运行这些代码就看到一个效果,红色方块旋转的时候,只看到一半在旋转,就好像蓝色矩形遮住了一样。

如果改成以下代码

// 头像_headImageView = [[UIImageViewalloc] init];

  _headImageView.frame= CGRectMake(0,0,100,100);    _headImageView.backgroundColor= [UIColorredColor];

  [_pmainPicture addSubview:_headImageView];

这样重新运行代码,红色方块绕着Y轴旋转,没有被蓝色矩形遮住了。

这两段代码的区别是,前者红色方块与蓝色矩形在同一个父窗口上(self.view),后者红色方块与蓝色矩形不在同一个父窗口。

UIView是iOS系统中界面元素的基础,所有的界面元素都继承自它。它本身完全是由CoreAnimation来实现的。它真正的绘图部分,是由一个叫CALayer(Core Animation Layer)的类来管理。UIView本身,更像是一个CALayer的管理器,访问它的跟绘图和跟坐标有关的属性,例如frame,bounds等等,实际上内部都是在访问它所包含的CALayer的相关属性。

前者红色方块与蓝色矩形同在同一layer层,所以红色方块做动画时,影响了蓝色矩形。

你可能感兴趣的:(加一个延着y轴翻转的动画)