【问题标题】:Rotate UIView and keep aspect ratio after rotation旋转 UIView 并在旋转后保持纵横比
【发布时间】:2012-02-28 08:26:14
【问题描述】:

我想旋转 UIView,但旋转后视图会调整大小。我正在使用自动调整大小标志“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)];

}

这里是旋转视图的延迟初始化。

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

}

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

【问题讨论】:

    标签: objective-c uiview rotation


    【解决方案1】:

    如果要设置非恒等变换矩阵,则需要设置 boundscenter。根据the docs

    警告如果此属性不是恒等变换,则 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)];
    }
    

    【讨论】:

    • 这并没有真正帮助。它仍在调整大小。
    • 嗯。你能告诉我们rotatingView是什么吗?它是如何定义的,等等?
    • 用我的旋转视图的惰性初始化代码更新了原始帖子。
    • 如果在setVerticalLabelFrame: 中设置中心和边界之前将rotatingView 的变换设置为CGAffineTransformIdentity,会发生什么?
    • 非常感谢先生!这有帮助。您能否将其添加到您的答案中,然后我接受并评价它! Nvm,加了!
    猜你喜欢
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 2012-06-17
    • 2019-10-16
    • 1970-01-01
    相关资源
    最近更新 更多