使用多个UIView获得正确的透视图

使用多个UIView获得正确的透视图,uiview,rotation,perspective,Uiview,Rotation,Perspective,我想在两个独立的并排视图方块上实现适当的透视“倾斜”。在下面的图像中,红色和绿色方块是应用相同变换的单独UIView。从视觉上看,这个透视图是不正确的(是吗?),或者至少黄色/蓝色的方形视图显示了优越的错觉。黄蓝色方块实际上是矩形父视图的子视图,变换应用于父视图 代码如下: @interface PEXViewController () @property (strong, nonatomic) IBOutlet UIView *redSquare; @property (strong, no

我想在两个独立的并排视图方块上实现适当的透视“倾斜”。在下面的图像中,红色和绿色方块是应用相同变换的单独UIView。从视觉上看,这个透视图是不正确的(是吗?),或者至少黄色/蓝色的方形视图显示了优越的错觉。黄蓝色方块实际上是矩形父视图的子视图,变换应用于父视图

代码如下:

@interface PEXViewController ()
@property (strong, nonatomic) IBOutlet UIView *redSquare;
@property (strong, nonatomic) IBOutlet UIView *greenSquare;
@property (strong, nonatomic) IBOutlet UIView *yellowSquareBlueSquare;

@end

@implementation PEXViewController

#define TILT_AMOUNT 0.65

-(void)tiltView:(UIView *)slave{
    CATransform3D rotateX = CATransform3DIdentity;
    rotateX.m34 = -1 / 500.0;
    rotateX = CATransform3DRotate(rotateX, TILT_AMOUNT * M_PI_2, 1, 0, 0);
    slave.layer.transform = rotateX;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self tiltView:self.greenSquare];
    [self tiltView:self.redSquare];
    [self tiltView:self.yellowSquareBlueSquare];
}

@end
1) 是否有一种简单的方法将变换应用于单独的红色/绿色UIView,并实现与“分组”黄色和蓝色UIView相同的效果?我更喜欢将视图分开,因为这是一个通用的应用程序,UIView在(例如)iPad布局中不是并排的

2) 如果#1是不可能的,我猜最好的办法就是简单地创建一个父视图,它出现在iPhone中,但不出现在iPad中。还有其他选择吗?

我选择了解决方案2。我创建了一个简短的例程,该例程基于UIView数组计算边界框,从边界框创建新的父视图,然后将阵列视图添加为子视图。然后,我可以将变换应用于父视图以获得所需的效果。下面是收集和采用子视图的代码

-(UIView *)makeParentWithSubviews:(NSArray *)arrayOfViews{
    // Creating a bounding box UIView and add the passed UIViews as subview
    // "in-place".

    CGFloat xMax = -HUGE_VALF;
    CGFloat xMin =  HUGE_VALF;
    CGFloat yMax = -HUGE_VALF;
    CGFloat yMin = HUGE_VALF;
    for (UIView *myView in arrayOfViews) {

        xMin = MIN(xMin, myView.frame.origin.x);
        xMax = MAX(xMax, myView.frame.origin.x + myView.frame.size.width);
        yMin = MIN(yMin, myView.frame.origin.y);
        yMax = MAX(yMax, myView.frame.origin.y + myView.frame.size.height);
    }

    CGFloat parentWidth = xMax - xMin;
    CGFloat parentHeight = yMax - yMin;

    CGRect parentFrame = CGRectMake(xMin, yMin, parentWidth, parentHeight);
    UIView *parentView = [[UIView alloc] initWithFrame:parentFrame];

    // Replace each child's frame
    for (UIView *myView in arrayOfViews){
        myView.frame = [[myView superview] convertRect:myView.frame toView:parentView];
        [myView removeFromSuperview];
        [parentView addSubview:myView];
    }

    parentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:parentView];
    return parentView;
}