2013-01-13 42 views
1

我试图在UILabel上添加一些透视图,使其显示为如果它被涂在斜面上。UILabel上的透视变换

这里是我的测试标签:

Desc

和应用后的变换:

enter image description here

我的代码:

@implementation MyViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100.f, 250.f, 100.f, 30.f)]; 
    label.text = @"Hello"; 
    label.textAlignment = NSTextAlignmentCenter; 
    label.backgroundColor = [UIColor greenColor]; 
    [label tiltDegrees:20.f]; 
    [self.view addSubview:label]; 
    [label release]; 
} 

@end 


@implementation UIView (Perspective) 

-(void) tiltDegrees:(CGFloat)degrees { 
    CATransform3D aTransform = CATransform3DIdentity; 
    CGFloat zDistance = 100; // affects the sharpness of the transform 
    aTransform.m34 = 1.0/-zDistance; 
    aTransform = CATransform3DRotate(aTransform, degrees * M_PI/180.0f, 1.0f, 0.0f, 0.0f); 
    self.layer.transform = aTransform; 
} 

@end 

我坚持用B问题unch:标签内的文字似乎

  1. 并不适用改造以同样的方式作为UILabel本身一样。它超出框架(剪裁),不居中,几何看起来不正确。也许大小也是错误的?
  2. 它模糊。如何在之前将文本向量上的变换应用于光栅化。我认为这会产生清晰的结果。
  3. 我似乎无法计算出zDistancedegrees之间的协调关系,以便它们产生正确的视角。我如何根据degrees正确计算zDistance

任何帮助解决(1)(2)(3)将不胜感激。 很多谢谢

回答

2

我不知道你是否已经解决了这个问题,但问题基本上是你的文本标签正在穿过背后的灰色层,因此它似乎夹在文本中间。

将文本层转换为相机以解决此问题。