[英]Shake gesture works on simulator, but not iPhone
I have some code in my app which I use to detect shake, once the device is shaken the background image is changed, some elements on screen are hidden & some are unhidden. 我的应用程序中有一些代码可用来检测抖动,一旦设备抖动,背景图片就会发生变化,屏幕上的某些元素被隐藏,而另一些则未被隐藏。 If you shake it again it returns back to the original display... this works perfectly on the simulator, but not at all on my actual iPhone 5S! 如果再次摇动它,它会返回到原始显示...这在模拟器上可以完美运行,但在我的实际iPhone 5S上却根本不起作用!
If I shake the iPhone, I see the changes flash up but it then goes back to the original view in less than a second. 如果摇动iPhone,我会看到更改闪烁,但是不到一秒钟便返回到原始视图。 Here is my code: 这是我的代码:
- (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);
}];
}
}
}
Is this an issue with my code? 这是我的代码有问题吗? Or am I simply trying to do something that is not possible? 还是我只是试图做一些不可能的事情?
Ok - I threw a couple of NSLogs in there at each shake & on detection, worked out the issue is when the 1st if statement is true the 2nd if statement then becomes true so runs immediately afterwards! 好的-在每次摇动和检测时,我都在其中扔了几个NSLogs,解决了问题是当第一个if语句为true时第二个if语句为true时,然后立即运行! So I changed the if's to else if's & its sorted - simulator was working because i was using the 3.5" sim which already had else-if statement.... my bad :( 所以我将if改为else if的排序方式-模拟器正在运行,因为我使用的是3.5“ sim卡,它已经具有else-if语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.