使用多个UIViews获得正确的视角

时间:2013-07-31 21:12:25

标签: uiview rotation perspective

我希望在两个独立的UIView正方形上实现正确的“倾斜”视角。在下图中,红色和绿色方块是单独的UIViews,应用了相同的变换。在视觉上这个视角是不正确的(是吗?),或至少黄色/蓝色方形UIViews显示出优越的幻觉。黄色 - 蓝色方块实际上是矩形父UIView的子视图,并且转换应用于父视图。

Transforming single UIView vs. a parent 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)是否有一种简单的方法将变换应用于单独的红色/绿色UIViews,并实现与“分组”黄色和蓝色UIViews相同的效果?我更喜欢将视图分开,因为这是一个通用的应用程序,UIViews并不是(例如)iPad布局中的并排。

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

1 个答案:

答案 0 :(得分:0)

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

-(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;
}
相关问题