iphone';s旋转视图没有';不要占整个屏幕
我使用CGAffineTransformMakeRotation旋转了一个视图。() 如下图所示,图像的左右两侧都有白色区域。iphone';s旋转视图没有';不要占整个屏幕,iphone,view,rotation,Iphone,View,Rotation,我使用CGAffineTransformMakeRotation旋转了一个视图。() 如下图所示,图像的左右两侧都有白色区域。 我希望图像占据整个空间,背景为黑色。(至少在一维、宽度或高度上) 下面是完整的代码 - (void) viewDidLoad { [super viewDidLoad]; self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexible
我希望图像占据整个空间,背景为黑色。(至少在一维、宽度或高度上) 下面是完整的代码
- (void) viewDidLoad
{
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor = [UIColor blackColor];
self.imageView.backgroundColor = [UIColor blackColor];
self.imageView.opaque = NO;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.imageView setImageWithURL:[NSURL URLWithString:self.jsonAlbumImage.url_image
relativeToURL: [NSURL URLWithString:@URL_BASE]]
placeholderImage: [GlobalHelper placeHolderImage]];
[self.view addSubview: self.imageView];
}
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
} else if (orientation == UIDeviceOrientationLandscapeRight) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI)];
} else if (orientation == UIDeviceOrientationPortrait) {
[self.view setTransform:CGAffineTransformMakeRotation(0.0)];
}
}
--编辑--
最终对我有用的是。。不知道为什么修改会起作用。。任何解释都很好
UIDeviceOrientation orientation = [[notification object] orientation];
CGAffineTransform t;
CGRect rect;
if (orientation == UIDeviceOrientationLandscapeLeft) {
t = CGAffineTransformMakeRotation(M_PI / 2.0);
rect = CGRectMake(0,0,480,320);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
t = CGAffineTransformMakeRotation(M_PI / -2.0);
rect = CGRectMake(0,0,480,320);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
t = CGAffineTransformMakeRotation(M_PI);
rect = CGRectMake(0,0,320,480);
} else if (orientation == UIDeviceOrientationPortrait) {
t = CGAffineTransformMakeRotation(0.0);
rect = CGRectMake(0,0,320,480);
}
else
return; // looks like there are other orientations than the specified 4
[self.view setTransform:t];
self.view.bounds = rect;
在-(void)didRotate:(NSNotification*)notification
中,您需要重新发布视图,以便它们占据所有可用空间。你可以这样做:
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
CGAffineTransform t;
if (orientation == UIDeviceOrientationLandscapeLeft) {
t = CGAffineTransformMakeRotation(M_PI / 2.0);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
t = CGAffineTransformMakeRotation(M_PI / -2.0);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
t = CGAffineTransformMakeRotation(M_PI);
} else if (orientation == UIDeviceOrientationPortrait) {
t = CGAffineTransformMakeRotation(0.0);
}
CGPoint screenCenter = CGPointMake([UIScreen mainScreen].bounds.width/2,[UIScreen mainScreen].bounds.height/2);
self.view.center = CGPointApplyAffineTransform(screenCenter, t);
self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
[self.view setTransform:t];
}
其中:
CGRectApplyAffineTransform
将仿射变换应用于矩形
CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);
请尝试删除此行:
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
如果你不打算使用自动旋转,你就不需要它,我担心它可能会与你自己设置的视图框架相冲突。这只是一个假设,用来解释你没有看到视图的框架变化这一事实
编辑:
我很想知道这个转换的功能
嗯,变换正在进行旋转
旋转相对于用作枢轴的锚定点;所发生的是,如果锚点不在视图旋转的中间,那么视图也被翻译(想象围绕顶点的旋转)。
因此,设定界限以使事情均衡是正确的;事实上,我只是想说:
self.view.center = CGPointApplyAffineTransform(screenCenter, t);
self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
但对中心和边界应用相同变换的想法可能并不可取。(这也是为什么我要求提供一些跟踪,以查看发生了什么:-)在-(void)didRotate:(NSNotification*)notification
中,您需要重新显示视图,以便它们占据所有可用空间。你可以这样做:
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
CGAffineTransform t;
if (orientation == UIDeviceOrientationLandscapeLeft) {
t = CGAffineTransformMakeRotation(M_PI / 2.0);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
t = CGAffineTransformMakeRotation(M_PI / -2.0);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
t = CGAffineTransformMakeRotation(M_PI);
} else if (orientation == UIDeviceOrientationPortrait) {
t = CGAffineTransformMakeRotation(0.0);
}
CGPoint screenCenter = CGPointMake([UIScreen mainScreen].bounds.width/2,[UIScreen mainScreen].bounds.height/2);
self.view.center = CGPointApplyAffineTransform(screenCenter, t);
self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
[self.view setTransform:t];
}
其中:
CGRectApplyAffineTransform
将仿射变换应用于矩形
CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);
请尝试删除此行:
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
如果你不打算使用自动旋转,你就不需要它,我担心它可能会与你自己设置的视图框架相冲突。这只是一个假设,用来解释你没有看到视图的框架变化这一事实
编辑:
我很想知道这个转换的功能
嗯,变换正在进行旋转
旋转相对于用作枢轴的锚定点;所发生的是,如果锚点不在视图旋转的中间,那么视图也被翻译(想象围绕顶点的旋转)。
因此,设定界限以使事情均衡是正确的;事实上,我只是想说:
self.view.center = CGPointApplyAffineTransform(screenCenter, t);
self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
但对中心和边界应用相同变换的想法可能并不可取。(这也是为什么我要寻找一些痕迹,看看发生了什么:-)它看起来占据了整个高度。如果视图的高度大于宽度,这将是所需的行为。不,顶部和底部的图像都没有黑色背景。是否为ImageView指定了适当的内容模式?记录控制器视图和图像视图的边界,并检查它们是否都是(320480),即使在旋转时也是如此。是,我尝试将480320设置为view.frame imageView.frame,但没有解决该问题。。也许我不应该设置框架的原点。。brbit看起来占据了整个高度。如果视图的高度大于宽度,这将是所需的行为。不,顶部和底部的图像都没有黑色背景。是否为ImageView指定了适当的内容模式?记录控制器视图和图像视图的边界,并检查它们是否都是(320480),即使在旋转时也是如此。是,我尝试将480320设置为view.frame imageView.frame,但没有解决该问题。。也许我不应该设置框架的原点。。brbcgrectApplyGaffineTransform的返回值为int。。请您解释一下为什么删除autoresizingMask会有帮助?对不起,正确的拼写是
CGRectApplyAffinetTransform
。有关详细信息,请参阅我编辑的答案。已尝试,但无效。。图像显示在除纵向旋转以外的其他旋转中。我记录了view/imageView的界限,这是(480320)一个好的开始。像我在编辑后的答案中建议的那样设置视图中心怎么样?如果我旋转到垂直方向(变为全白色),然后返回到纵向,图像就在那里..cRectApplyGaffineTransform的返回值是int。。请您解释一下为什么删除autoresizingMask会有帮助?对不起,正确的拼写是CGRectApplyAffinetTransform
。有关详细信息,请参阅我编辑的答案。已尝试,但无效。。图像显示在除纵向旋转以外的其他旋转中。我记录了view/imageView的界限,这是(480320)一个好的开始。像我在编辑后的答案中建议的那样设置视图中心怎么样?如果我旋转到垂直(变为全白色),然后返回到纵向,图像就在那里。。