我正在编写一个应用程序来检查设备的初始物理位置。如果它是平的,设备应该做一些事情,然后用户应该将设备定位到其他位置,这样应用程序就可以做额外的工作。如果应用程序不是平坦的,则用户应该将位置调整为平坦,然后根据应用程序的要求继续在不同的位置移动设备。
无论设备最初是否是扁平的,或者如果用户必须移动设备使其扁平,这两个操作都应该调用相同的方法,这些方法会导致一系列需要处理的if/else语句。以下是检查设备的初始物理方向的方法:
- (void) accelerometerCheck {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown) {
currentTest++;
[self orientationChanged:nil];
}
else {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
}
}currentTest是一组枚举,每次设备的方向改变时,它们的值都必须改变。每次设备改变位置时,将执行一次新的测试,总共进行三次测试(平面、横向和纵向测试)。上面的代码块调用的方法如下所示:
- (void) orientationChanged:(NSNotification *)note {
if (note == nil) {
device = [UIDevice currentDevice];
}
else
device = note.object;由于某种原因,我的代码没有达到下面的'if‘语句
if(currentTest == flatTest) {
if (device.orientation == UIDeviceOrientationFaceUp || device.orientation == UIDeviceOrientationFaceDown) {
//do something
}
else {
//do something other stuff
}
}
// If the device is initially in the flat position, then it should
// call the method "orientationChanged" and come directly to this
// point in the method...which it is not doing.
else if (currentTest == landscapeTest) {
if (device.orientation == UIDeviceOrientationLandscapeLeft || device.orientation == UIDeviceOrientationLandscapeRight) {
//do some tests
}
else {
}
currentTest++;
}
else if (currentTest == portraitTest) {
if (device.orientation == UIDeviceOrientationPortrait || device.orientation == UIDeviceOrientationPortraitUpsideDown) {
//do more tests
}
else {
return;
}
currentTest++;
}
else if(currentTest == doneTest) {
//give test results to the user
}
else
[self testFail];
}
else if(currentTest == timeoutTest) {
//Do something to tell the user the test timed out
}
}有没有人看到我做错了什么?我的感觉是,我没有将正确的NSNotification参数值传递给方法" orientationChanged ",或者由于某种原因,我无法将orientationChanged方法中的前两个if/else语句连接到后面的if/else语句。
发布于 2013-01-08 14:16:44
尝试在每个条件中设置断点,并查看对于设备方向的每个更改,您可以轻松地找到错误所在的编码working.Then。
https://stackoverflow.com/questions/14179844
复制相似问题