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

在iOS开发中,作者发现一个红色方块在做绕Y轴旋转动画时,仅显示一半,似乎被其在同一父视图的蓝色矩形遮挡。经过调试,发现将两者放在不同父视图时,动画显示正常。问题的关键在于,当红色方块与蓝色矩形位于同一UIView时,它们共享同一Core Animation Layer,导致动画效果互相影响。理解UIView与CALayer的关系对于解决此类问题至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图片中蓝色矩形作为背景,当点击按钮时红色方形绕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层,所以红色方块做动画时,影响了蓝色矩形。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值