【问题标题】:Getting proper perspective with multiple UIViews使用多个 UIView 获得正确的视角
【发布时间】:2013-07-31 21:12:24
【问题描述】:

我想在两个单独的并排 UIView 方块上实现适当的透视“倾斜”。在下图中,红色和绿色方块是应用了相同变换的单独 UIView。从视觉上看,这个视角是不正确的(是吗?),或者至少黄色/蓝色方形 UIView 显示了优越的错觉。黄色-蓝色方块实际上是矩形父 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 中。还有其他选择吗?

【问题讨论】:

    标签: uiview rotation perspective


    【解决方案1】:

    我选择了解决方案 #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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 2012-01-20
      相关资源
      最近更新 更多