Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Same Method to Animate Different Images

FalimondFalimond Posts: 16Registered Users
edited September 2011 in iPhone SDK Development
Hello All,

I've got a method (IBAction)toggleState which is triggered by a custom button that sits atop a square image. The image is a representation of the button and can either be black or white. When the button is pressed an animation of that button flipping over can be seen. I have this working perfectly, with one button.

Now I need to implement an array of 6x8 button images each with its own custom button where a button tap will animate the corresponding button image as well as any surrounding button images of my choosing. All the button states must be saved and updated after each method call.

Here's what I have for a single button implementation. The button image starts off white and with a state of -1.



*Problem solved...thanks!
Post edited by Falimond on

Replies

  • dapisdapis Posts: 307Registered Users
    edited September 2011
    Falimond;370475 said:
    Hello All,

    I've got a method (IBAction)toggleState which is triggered by a custom button that sits atop a square image. The image is a representation of the button and can either be black or white. When the button is pressed an animation of that button flipping over can be seen. I have this working perfectly, with one button.

    Now I need to implement an array of 6x8 button images each with its own custom button where a button tap will animate the corresponding button image as well as any surrounding button images of my choosing. All the button states must be saved and updated after each method call.

    Here's what I have for a single button implementation. The button image starts off white and with a state of -1.


    int i = -1;

    -(IBAction)toggleState:(id)sender
    {

    switch(i){

    case 1: {
    NSArray *flip_to_white = [[NSArray alloc] initWithObjects:
    [UIImage imageNamed:@\"flip_black_1.png\"],
    .
    .
    .
    [UIImage imageNamed:@\"flip_white_1.png\"], // item 58 - white button image
    nil ];

    buttonImage.animationImages = flip_to_white;
    buttonImage.animationDuration = .3;
    buttonImage.animationRepeatCount = 1;
    buttonImage.image = [flip_to_white objectAtIndex: 58];
    [buttonImage startAnimating];

    i = (i*-1);
    break;
    }

    case -1: {
    NSArray *flip_to_black = [[NSArray alloc] initWithObjects:
    [UIImage imageNamed:@\"flip_white_1.png\"],
    .
    .
    .
    [UIImage imageNamed:@\"flip_black_1.png\"], // item 58 - black button image
    nil ];

    buttonImage.animationImages = flip_to_black;
    buttonImage.animationDuration = .3;
    buttonImage.animationRepeatCount = 1;
    buttonImage.image = [flip_to_black objectAtIndex: 58];
    [buttonImage startAnimating];
    i = (i*-1);
    break;
    }
    }

    }



    I've given up solving it on my own :/ I have experience with C but only started iPhone dev. this week. I could define a different method for each button and control other button images in each of those methods... but there has to be a simpler way.
    You can deal with the same code for multiple images by naming the images in numerical order such as myImage1.png, myImage2.png, etc.

    Then use something akin to the following - where your code has provided the int k as the number of the desired image:

    UIImage *theImageToUse =
    imageNamed:[NSString stringWithFormat:@"myImage%d.png", k]
  • FalimondFalimond Posts: 16Registered Users
    edited September 2011
    I just placed the animation arrays at the beginning of the method definition that way I can just reuse flip_to_white/black throughout the method.

    To cause different images to flip I've done this.

    UIImageView *imageFlipped = image0;

    if(A[i] == 1) {
    imageFlipped.animationImages = flip_to_white;


    I got this working and it will cause image0 to flip. Is there a way to define imageFlipped with different images in a loop? Something along the lines of....



    for(i=0;i < 4;i++){
    x = A[i];
    UIImageView *imageFlipped = image<x>;

    if(A[i] == 1) {
    imageFlipped.animationImages = flip_to_white;
    ..... }

    }


    Where A[i] is an array containing the image values that I want flipped.
Sign In or Register to comment.