views:

170

answers:

1

Hi all, I have an odd question here. I have two animations set up for a simple page flip. I then have a button that randomly generates a 1 or a 0. If the number is 0 then it performs one animation, if its 1 it performs another.

The issues I'm seeing is that this all runs fine on the simulator, but on the device it doesn't perform the animation if the random number is 0.

Any clues?

    -(IBAction)pageTurn:(id)sender {
    int randomNumber = arc4random() %2;
    NSLog(@"randomNumber = %d", randomNumber);
    if (randomNumber == 0) {
        [self turnPageForward];
    } else {
        [self turnPageBackward];
    }
}

-(void)turnPageForward {
    NSArray *myImages = [NSArray arrayWithObjects:
                         [UIImage imageNamed:@"PageTurnOne.png"],
                         [UIImage imageNamed:@"PageTurnTwo.png"],
                         [UIImage imageNamed:@"PageTurnThree.png"],
                         [UIImage imageNamed:@"PageTurnFour.png"],
                         [UIImage imageNamed:@"PageTurnFive.png"],
                         [UIImage imageNamed:@"PageTurnSix.png"],
                         [UIImage imageNamed:@"PageTurnSeven.png"],
                         [UIImage imageNamed:@"PageTurnEight.png"],
                         [UIImage imageNamed:@"PageTurnNine.png"],
                         nil];

    UIImageView *myAnimatedView = [UIImageView alloc];
    [myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 480)];
    myAnimatedView.animationImages = myImages;
    myAnimatedView.animationDuration = .5; // seconds
    myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
    [myAnimatedView startAnimating];
    [self.view addSubview:myAnimatedView];
    [myAnimatedView release]; 
}

-(void)turnPageBackward {
    NSArray *myImages = [NSArray arrayWithObjects:
                         [UIImage imageNamed:@"PageTurnNine.png"],
                         [UIImage imageNamed:@"PageTurnEight.png"],
                         [UIImage imageNamed:@"PageTurnSeven.png"],
                         [UIImage imageNamed:@"PageTurnSix.png"],
                         [UIImage imageNamed:@"PageTurnFive.png"],
                         [UIImage imageNamed:@"PageTurnFour.png"],
                         [UIImage imageNamed:@"PageTurnThree.png"],
                         [UIImage imageNamed:@"PageTurnTwo.png"],
                         [UIImage imageNamed:@"PageTurnOne.png"],
                         nil];

    UIImageView *myAnimatedView = [UIImageView alloc];
    [myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 480)];
    myAnimatedView.animationImages = myImages;
    myAnimatedView.animationDuration = .5; // seconds
    myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
    [myAnimatedView startAnimating];
    [self.view addSubview:myAnimatedView];
    [myAnimatedView release]; 
}
+1  A: 

Usually, when code fails on the device but not the simulator (or vice versa) the culprit is a library or other compiled source that was compiled for one hardware but not the other.

Another related possibility is that a resource was not added properly so that it is not included in the final build for the device. I would check the image files in the method that does not work.

You should also confirm if the turnPageForward is being called at all. That will give you a clue as to where the problem lays.

TechZen
Thanks for the reply. I've checked, and turnPageForward is definitely getting called. I even tried moving that code into the IBAction but it made no difference.The resources should all be added properly, as both animations are using the same images only in a different order.I've also tried renaming one of the arrays and one of the animations.
Alan Taylor
Yes, well "should be added properly" might be an unwarranted assumption. After all, the method "should work". ;-) You need to confirm they are being loaded by logging the array to see if it is populated by images and if they're the images you need. You could try swapping the specific images between the methods to see if the problem does follow the images. I think the fact that it fails on the device but not the simulator is key. We're looking for something that differs between the two builds.
TechZen
Well, the images are exactly the same, just in a different order. Surely if there was an issue with how they were added to the project it wouldn't work either way?I've tried doing it a few different ways, but only the order of Nine to One makes an animation, One to Nine just doesn't work for some strange reason :/
Alan Taylor
OK, after a LOT of playing around with this, it turns out that for some reason the device doesn't like the PageTurnOne image. When ever it reaches this one, it bails out of the animation. Turns out that when going in the reverse order it just wasn't so noticeable to miss that single frame on the end.Thanks techZen for all your help!
Alan Taylor
Glad I could help. Sometime you just need to bounce something off another person to get the problem out of your own head and out where you can see.
TechZen