旋转UIView并在旋转后保持纵横比

时间:2012-02-28 08:26:14

标签: objective-c uiview rotation

我想旋转UIView,但在旋转后调整视图大小。我正在使用autoresizing标志'UIViewAutoresizingNone'。

这是我的代码:(来自layoutSubViews

- (void) setVerticalLabelFrame:(CGRect)r {

r.origin.y +=100.0f;
r.size.height = 20.0f;
r.size.width = 180.0f;
[[self rotatatingView] setFrame:r];
//[[self rotatatingView] setTransform:CGAffineTransformMakeRotation(M_PI / 4.0f)];

}

这是rotateView的延迟初始化。

- (UIView*)rotatatingView {
if (rotatatingView == nil) {
    rotatatingView = [[UIView alloc] initWithFrame:CGRectZero];
    [rotatatingView setBackgroundColor:[UIColor orangeColor]];
    [rotatatingView setAutoresizingMask:UIViewAutoresizingNone];
    [[self imageView] addSubview:rotatatingView];
}
return rotatatingView;

}

第一个镜头是最后一行注释,第二个镜头是行未注释。 有什么想法吗?

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

您需要设置bounds& center如果您要设置非身份转换矩阵。根据{{​​3}}:

  

警告如果此属性不是标识转换,则frame属性的值未定义,因此应忽略。

所以尝试这样的事情:

- (void) setVerticalLabelFrame:(CGRect)r {
    CGRect bounds = CGRectZero;
    bounds.size.height = 20.0f;
    bounds.size.width = 180.0f;

    CGPoint center;
    center.x = r.origin.x + (bounds.size.width / 2.0f);
    center.x = r.origin.y + (bounds.size.height / 2.0f) + 100.0f;

    [[self rotatingView] setTransform:CGAffineTransformIdentity];

    [[self rotatingView] setCenter:center];
    [[self rotatingView] setBounds:bounds];

    [[self rotatingView] setTransform:CGAffineTransformMakeRotation(M_PI / 4.0f)];
}