在 UIView 内旋转 UIImageView 的奇怪行为
很多相关的问题,但我看不到任何与此完全匹配的问题。
我的应用程序有一个内容视图和一个广告横幅视图。
每次用户旋转设备时,
- (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
现在没有 < code>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}
我想了解发生了什么。
A lot of related questions, but I can't see any that quite match this.
My app has a content view and an ad-banner view.
Every time the user rotates the device,
- (void) layoutSubviews
gets called on the content view.
My content view comprises a single image. I want this image to rotate along with the device ( it is a textured background, so I just want to rotate it 90° or 0° depending on whether the physical device is in portrait or landscape.
Here is the code:
//
// 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
Now without the layoutSubviews
override it works. Only it is squashing the image rather than rotating it.
But when I put in the second function it goes haywire.
Here is an output log from repeatedly rotating the device left on the simulator.
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}
Taking out the transform, it is reporting the center point correctly.
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}
I would like to understand what is going on. Can anyone shed some light?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于明白了。
首先我需要设置
然后每次方向改变迫使layoutSubviews时我都必须重新居中图像视图:
Finally got it.
Firstly I needed to set
Then I had to re-centre the image view each time the orientation change forces layoutSubviews: