CATransform3D旋转导致图像的一半消失。

21

我正在使用以下代码旋转图像,但是被旋转“离开”页面的一半图像(沿y轴向下)消失了。如何解决?heading以弧度表示。

    CALayer *layer = myUIImageView.layer;
    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
    rotationAndPerspectiveTransform.m34 = 1.0 / 500;
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, heading, 0.0f, 1.0f, 0.0f);
    layer.transform = rotationAndPerspectiveTransform;

看起来这似乎会影响z-index。这个UIImageView是UIView的一部分,该UIView部分地“在”另一个UIView后面,但旋转后,myUIImageView总是绘制在顶部。 - iPadDeveloper2011
3个回答

34

解决方法是适当设置所有图层的zPosition属性。感谢@Brad Larson,在这里的评论中提出了这个解决方案。当您开始使用CATransform3D时,似乎通过addSubview建立的正常zindex视图层次结构被抛弃了。


5
  layer.anchorPoint = CGPointMake(0.0, 0.0);

0

anchorPoint设置为{0.0, 0.0}也可以工作(正如Liam已经指出的那样)。

以下是完整的代码片段,用于在不改变图层在屏幕上位置的情况下更改anchorPoint

     layer.anchorPoint = CGPointMake( 0.0, 0.0 );

     CGPoint position = layer.position;
     CGSize size = layer.bounds.size;

     CGFloat posX = position.x - size.width / 2.0;
     CGFloat posY = position.y - size.height / 2.0;

     layer.position = CGPointMake( posX, posY );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接