做一个绕Y轴旋转的动画时遇到问题

来源:互联网 发布:sqlalchemy sql 编辑:程序博客网 时间:2024/06/01 09:05

图片中蓝色矩形作为背景,当点击按钮时红色方形绕Y轴旋转

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

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    // image view    _pmainPicture=[[UIImageView alloc]initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 190.0f)];    _pmainPicture.backgroundColor = [UIColor blueColor];    _pmainPicture.contentMode = UIViewContentModeScaleAspectFill;    _pmainPicture.userInteractionEnabled = YES;    [self.view addSubview:_pmainPicture];    //button    _pButton = [UIButton buttonWithType:UIButtonTypeCustom];    [_pButton setTitle: @"请点击" forState: UIControlStateNormal];    _pButton.frame = CGRectMake(_pSwitch.frame.origin.x, _pSwitch.frame.origin.y+100, 200, 44);    _pButton.backgroundColor = [UIColor blueColor];    [_pButton setTitleColor: [UIColor redColor] forState: UIControlStateNormal];    [_pButton addTarget: self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    _pButton.accessibilityIdentifier = @"myButton";    [self.view addSubview: _pButton];    // 头像    _headImageView = [[UIImageView alloc] init];    _headImageView.frame = CGRectMake(0, 100, 100, 100);    _headImageView.backgroundColor = [UIColor redColor];    [self.view addSubview:_headImageView];}

这是按钮点击方法

-(void) buttonAction:(UIButton*) button{    [self rotate360WithDuration:2.0 repeatCount:1];    _headImageView.backgroundColor = [UIColor redColor];    _headImageView.animationDuration = 2.0;    _headImageView.animationRepeatCount = 1;    [_headImageView startAnimating];}

这是绕Y轴旋转的动画

- (void)rotate360WithDuration:(CGFloat)aDuration repeatCount:(CGFloat)aRepeatCount{    CAKeyframeAnimation *theAnimation = [CAKeyframeAnimation animation];    theAnimation.values = [NSArray arrayWithObjects:                           [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.layer addAnimation:theAnimation forKey:@"transform"];}

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

如果改成以下代码

// 头像    _headImageView = [[UIImageView alloc] init];    _headImageView.frame = CGRectMake(0, 0, 100, 100);    _headImageView.backgroundColor = [UIColor redColor];    [_pmainPicture addSubview:_headImageView];

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

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

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

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

0 0
原创粉丝点击