我的应用程序中有一些代码用于检测抖动,一旦设备被抖动,背景图像被更改,屏幕上的某些元素被隐藏&有些是不被隐藏的。如果再次摇晃它会返回到原来的显示器......这在模拟器上完美运行,但在我的实际iPhone 5S上完全没有!
如果我摇动iPhone,我会看到更改闪烁,但它会在不到一秒的时间内回到原始视图。这是我的代码:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
UIImage *iPhone5backgroundDO = [UIImage imageNamed:@"image1.png"];
UIImage *iPhone4backgroundDO = [UIImage imageNamed:@"image2.png"];
UIImage *iPhone5background = [UIImage imageNamed:@"image3.png"];
UIImage *iPhone4background = [UIImage imageNamed:@"image4.png"];
if (_backgroundImage.image == iPhone5background) {
[UIView animateWithDuration:0.5 animations:^(void) {
self.backgroundImage.image = iPhone5backgroundDO;
_yearsOut.alpha = 0.0;
_yearsLabel.alpha = 0.0;
_monthsOut.alpha = 0.0;
_monthsLabel.alpha = 0.0;
_daysOut.alpha = 0.0;
_hoursOut.alpha = 0.0;
_minsOut.alpha = 0.0;
_secsOut.alpha = 0.0;
_daysOnlyOut.alpha = 1.0;
_daysLabel.frame = CGRectMake(139, 273, 42, 21);
_hoursOutDO.alpha = 1.0;
_hoursLabel.frame = CGRectMake(41, 374, 52, 21);
_minsOutDO.alpha = 1.0;
_minsLabel.frame = CGRectMake(126, 374, 66, 21);
_secsOutDO.alpha = 1.0;
_secsLabel.frame = CGRectMake(212, 374, 71, 21);
}];
} if (_backgroundImage.image == iPhone5backgroundDO) {
[UIView animateWithDuration:0.5 animations:^(void) {
self.backgroundImage.image = iPhone5background;
_yearsOut.alpha = 1.0;
_yearsLabel.alpha = 1.0;
_monthsOut.alpha = 1.0;
_monthsLabel.alpha = 1.0;
_daysOut.alpha = 1.0;
_hoursOut.alpha = 1.0;
_minsOut.alpha = 1.0;
_secsOut.alpha = 1.0;
_daysOnlyOut.alpha = 0.0;
_daysLabel.frame = CGRectMake(226, 331, 42, 21);
_hoursOutDO.alpha = 0.0;
_hoursLabel.frame = CGRectMake(45, 457, 52, 21);
_minsOutDO.alpha = 0.0;
_minsLabel.frame = CGRectMake(125, 457, 66, 21);
_secsOutDO.alpha = 0.0;
_secsLabel.frame = CGRectMake(210, 457, 71, 21);
}];
} if (_backgroundImage.image == iPhone4background) {
[UIView animateWithDuration:0.5 animations:^(void) {
self.backgroundImage.image = iPhone4backgroundDO;
_yearsOut.alpha = 0.0;
_yearsLabel.alpha = 0.0;
_monthsOut.alpha = 0.0;
_monthsLabel.alpha = 0.0;
_daysOut.alpha = 0.0;
_hoursOut.alpha = 0.0;
_minsOut.alpha = 0.0;
_secsOut.alpha = 0.0;
_daysOnlyOut.alpha = 1.0;
_daysLabel.frame = CGRectMake(139, 273, 42, 21);
_hoursOutDO.alpha = 1.0;
_hoursLabel.frame = CGRectMake(43, 374, 52, 21);
_minsOutDO.alpha = 1.0;
_minsLabel.frame = CGRectMake(127, 374, 66, 21);
_secsOutDO.alpha = 1.0;
_secsLabel.frame = CGRectMake(213, 374, 71, 21);
}];
} else if (_backgroundImage.image == iPhone4backgroundDO) {
[UIView animateWithDuration:0.5 animations:^(void) {
self.backgroundImage.image = iPhone4background;
_yearsOut.alpha = 1.0;
_yearsLabel.alpha = 1.0;
_monthsOut.alpha = 1.0;
_monthsLabel.alpha = 1.0;
_daysOut.alpha = 1.0;
_hoursOut.alpha = 1.0;
_minsOut.alpha = 1.0;
_secsOut.alpha = 1.0;
_daysOnlyOut.alpha = 0.0;
_daysLabel.frame = CGRectMake(212, 271, 45, 21);
_hoursOutDO.alpha = 0.0;
_hoursLabel.frame = CGRectMake(57, 371, 49, 21);
_minsOutDO.alpha = 0.0;
_minsLabel.frame = CGRectMake(122, 371, 66, 21);
_secsOutDO.alpha = 0.0;
_secsLabel.frame = CGRectMake(198, 371, 71, 21);
}];
}
}
}
这是我的代码的问题吗?或者我只是想做一些不可能的事情?
答案 0 :(得分:0)
好的 - 我在每次摇晃时都扔了几个NSLog。在检测时,解决了问题是当第一个if语句为真时,第二个if语句然后变为真,所以之后立即运行!所以我将if改为else,如果是&它的排序模拟器正在工作,因为我使用的是3.5“sim已经有了else-if语句....我的坏:(