xcode UIButton 中的动画图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12608286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:39:21  来源:igfitidea点击:

Animated Images in a UIButton

iosxcodeuibuttonuiviewanimation

提问by Cherr Skees

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?

假设我想循环播放 6 帧并且让用户仍然可以单击按钮?

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?

有没有办法在定义内为它们设置动画UIButton

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

还是将图像动画化并找到用户接触点并对其采取行动更好?

回答by Mick MacCallum

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:

您可以使用 的 imageView 属性执行此操作UIButton。这将允许按钮像往常一样运行您的图像动画。下面是一个例子:

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];