Search code examples
ioscocoa-touchios4uibutton

How do I animate a UIButton between two PNG images?


I have two PNGs I want to use as a button. How can I animate a UIButton by switching rapidly between these two images?


Solution

  • You can use animationImages property of your button's imageView:

    myButton.imageView.animationImages =
    [NSArray arrayWithObjects:[UIImage imageNamed:@"image1.png"],
                              [UIImage imageNamed:@"image2.png"],
                              nil];
    myButton.imageView.animationDuration = 0.5; //whatever you want (in seconds)
    [myButton.imageView startAnimating];
    

    Your button will switch between your two images.

    EDIT: As @tidbeck pointed the button needs to have an image assigned to create the imageview property.