UIDeviceOrientationDidChangeNotification 在设备改变方向时多次调用。我不确定为什么会发生这种情况或如何解决它。
我想要做的是保持滚动视图的 contentoffset 相同,因此当用户旋转屏幕时,应用程序会保留他们所在的页面。
奇怪的是,当我第一次像我想要的那样执行代码时旋转屏幕。但之后每次代码都会执行多次,最终 contentoffset 设置为 0。
这就是我所拥有的。
- (void)loadView {
//some code that sizes itself depending on the current orientation
//WILL BE CALLED AFTER EVERY ORIENTATION CHANGE
}
- (void)viewDidLoad {
//begin generating messages
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
//if is portrait and was landscape
if (orientation==1 && temp==2) {
int cal = offsetHolder.x/screenframe.size.height;
offsetHolder.x = cal * screenframe.size.width;
ScrollView.contentOffset = offsetHolder;
}
//if is landscape and was portrait
if (orientation==2 && temp==1) {
int cal = offsetHolder.x/screenframe.size.width;
offsetHolder.x = cal * screenframe.size.height;
ScrollView.contentOffset = offsetHolder;
}
}
在方向更改时,我更改“int方向”的值,然后调用 loadview 来更改视图的大小。然后我调用 viewdidload 来获取正确的 contentoffset
- (void) orientationChanged:(NSNotification *)note {
CGRect screenframe = [[UIScreen mainScreen] bounds];
//holding the current offset
offsetHolder = ScrollView.contentOffset;
if ([[UIDevice currentDevice] orientation] == 1 || [[UIDevice currentDevice] orientation] == 0 || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
temp=orientation;
orientation = 1;
[self loadView];
[self viewDidLoad];
}
else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationFaceDown || [[UIDevice currentDevice] orientation] == UIDeviceOrientationFaceUp){
temp=orientation;
}
else{
temp=orientation;
orientation = 2;
[self loadView];
[self viewDidLoad];
}
}
编辑:
我发现了问题。我正在做的是创建另一个 self.view 实例而不是覆盖这个实例。有没有简单的方法来破坏这个视图并重新初始化它?
编辑2:
找到了解决办法。根据 jsds 的说明,我停止调用 loadview 和 viewdidload。而是将我的 loadview 中的所有代码移动到我从 loadview 调用的另一个函数中。这段代码所做的只是实例化 UI (initview) 对象并根据方向将它们放置在正确的位置。
然后我创建另一个从视图中删除所有子视图的函数。然后在方向改变时,我调用这个函数和我的 initview 来销毁所有子视图,然后在方向改变时重新创建它们。