简体   繁体   中英

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!

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. 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! 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 :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM