我知道有很多关于它的帖子,但我找不到最好的解决方案。
我有一个“holder” View (UIView),其中包含许多在横向模式下水平拉伸(stretch)的 ScrollView 。每个 ScrollView 都包含垂直滚动的包含图像的 View 。同样,整件事都在风景中。
我想要的是,当我旋转到纵向模式时,包含所有内容的“holder” View 保持不变,意思是现在是一列, ScrollView 旋转意味着滚动是水平的,但是 ScrollView 的内容(包含图像的 View )旋转。
我尝试编写一个 UIView 子类(用于“holder” View )并放置以下方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
我希望以同样的方式处理驻留在我的“holder” View 中的 subview ,但这不起作用。最好的方法是什么?谢谢。
最佳答案
您可以将支持的方向设置为您想要的方向,然后观察 UIDevice
方向更改以手动处理其他方向。这里有一个例子:
#import "ViewController.h"
@interface ViewController ()
- (void)deviceDidRotate:(NSNotification *)notification;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceDidRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#pragma mark - Private methods
- (void)deviceDidRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
/* Handle manually the rotation
For instance, apply a transform to a UIView:
CGAffineTransform transform = CGAffineTransformMakeRotation(radians);
self.aView.transform = transform; */
}
@end
关于ios - 不要旋转 UIView,而是旋转其中的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836435/