UIButton 中的动画图像

Animated Images in a UIButton(UIButton 中的动画图像)

本文介绍了UIButton 中的动画图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为按钮图像设置动画的最佳方式是什么?

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

假设我想循环 6 帧,让用户仍然可以点击按钮?

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?

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

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?

推荐答案

您可以使用 UIButton 的 imageView 属性来做到这一点.这将允许按钮的行为与您的图像动画一样正常.这是一个例子:

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

这篇关于UIButton 中的动画图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIButton 中的动画图像