Animated Images in a UIButton

余生颓废 提交于 2019-12-18 12:18:10

问题


What is the best way to animate images of a button?

Say I want to cycle through like 6 frames and have the user still be able to click the button?

Should I animate the images and just have an invisible button on top of it in interface builder?

Is there a way to animate them within defining the UIButton?

Or is it better to animate images and find the users touch point and act on that?


回答1:


You can do this using the imageView property of UIButton. This will allow the button to behave as it normally would with your images animating on it. Here's an example:

NSMutableArray *imageArray = [NSMutableArray new];

for (int i = 1; i < 4; i ++) {
    [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
}

[myButton setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

[myButton.imageView setAnimationImages:[imageArray copy]];
[myButton.imageView setAnimationDuration:0.5];

[myButton.imageView startAnimating];


来源:https://stackoverflow.com/questions/12608286/animated-images-in-a-uibutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!