How do I animate a UIButton between two PNG images?

后端 未结 2 828
傲寒
傲寒 2020-12-05 10:54

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

相关标签:
2条回答
  • 2020-12-05 11:31

    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.

    0 讨论(0)
  • 2020-12-05 11:48

    Normally for a button you can set three "live" states: - Normal - Highlighted - Selected I don't know if this can help, but if you set one image to "Normal" and the other image to "Highlighted" you can see the two images while pushing the button. I don't know if this effect is enough for you.

    0 讨论(0)
提交回复
热议问题