UIButton中的动画图像 [英] Animated Images in a UIButton

查看:76
本文介绍了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中的动画图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆