【问题标题】:Strange behaviour rotating UIImageView inside UIView在 UIView 内旋转 UIImageView 的奇怪行为
【发布时间】:2011-08-16 03:20:18
【问题描述】:

很多相关的问题,但我看不到任何与此完全匹配的问题。

我的应用有一个内容视图和一个广告横幅视图。

每次用户旋转设备时,

- (void) layoutSubviews

在内容视图上被调用。

我的内容视图包含一个图像。我希望此图像与设备一起旋转(它是带纹理的背景,因此我只想将其旋转 90° 或 0°,具体取决于物理设备是纵向还是横向。

代码如下:

//
//  ContentView.m
//  ChordWheel2
//
//  Created by Pi on 16/08/2011.
//  Copyright 2011 Pi. All rights reserved.
//

#import "ContentView.h"


#import "Helper.h"

@implementation ContentView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        self.autoresizesSubviews = YES;
        {
            // background -- want to cache, so imageNamed is the right method
            UIImage* backgroundImage = [UIImage imageNamed: @"background2.png"];

            background = [[[UIImageView alloc] initWithFrame: frame] autorelease];

            background.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

            //background.autoresizingMask = 
            [background setImage: backgroundImage];

            [self addSubview: background];
        }
    }
    return self;
}

- (void) layoutSubviews
{
    UIDeviceOrientation O = [UIDevice currentDevice].orientation;

    BOOL is_L = UIInterfaceOrientationIsLandscape( O );

    float theta = is_L ? M_HALF_PI : 0;

    LOG( @"T = %@", NSStringFromCGAffineTransform( background.transform ) ) ;
    LOG( @"C = %@", NSStringFromCGPoint( background.center ) );

    background.transform = CGAffineTransformMakeRotation( theta );
}

@end

现在没有 layoutSubviews 覆盖它可以工作。只是它在压缩图像而不是旋转它。

但是当我输入第二个函数时,它变得混乱了。

这是重复旋转模拟器左侧设备的输出日志。

T = [1, 0, 0, 1, 0, 0]    C = {160, 240}
T = [1, 0, 0, 1, 0, 0]    C = {240, 160}
T = [0, 1, -1, 0, 0, 0]    C = {160, 240}
T = [1, 0, -0, 1, 0, 0]    C = {240, 160}
T = [0, 1, -1, 0, 0, 0]    C = {320, 80}
T = [1, 0, -0, 1, 0, 0]    C = {400, 0}
T = [0, 1, -1, 0, 0, 0]    C = {320, 80}
T = [1, 0, -0, 1, 0, 0]    C = {400, 0}
T = [0, 1, -1, 0, 0, 0]    C = {320, 80}
T = [1, 0, -0, 1, 0, 0]    C = {480, -80}
T = [0, 1, -1, 0, 0, 0]    C = {400, 0}
T = [1, 0, -0, 1, 0, 0]    C = {640, -80}
T = [0, 1, -1, 0, 0, 0]    C = {560, 0}
T = [1, 0, -0, 1, 0, 0]    C = {800, -80}
T = [0, 1, -1, 0, 0, 0]    C = {720, 0}
T = [1, 0, -0, 1, 0, 0]    C = {960, -80}
T = [0, 1, -1, 0, 0, 0]    C = {880, 0}

取出变换,它正在正确报告中心点。

T = [1, 0, 0, 1, 0, 0]    C = {160, 240}
T = [1, 0, 0, 1, 0, 0]    C = {240, 160}
T = [1, 0, 0, 1, 0, 0]    C = {160, 240}
T = [1, 0, 0, 1, 0, 0]    C = {240, 160}
T = [1, 0, 0, 1, 0, 0]    C = {160, 240}
T = [1, 0, 0, 1, 0, 0]    C = {240, 160}

我想了解发生了什么。有人能解释一下吗?

【问题讨论】:

    标签: ios uiview cgaffinetransform image-rotation autorotate


    【解决方案1】:

    终于明白了。

    首先我需要设置

    self.autoresizesSubviews = NO;
    

    然后,每次方向更改强制 layoutSubviews 时,我都必须重新居中图像视图:

    background.center = self.center;
    background.transform = CGAffineTransformMakeRotation( theta );
    

    【讨论】:

      猜你喜欢
      • 2011-08-13
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多