[英]How do I animate a UIButton between two PNG images?
I have two PNGs I want to use as a button.我有两个要用作按钮的 PNG。 How can I animate a UIButton by switching rapidly between these two images?如何通过在这两个图像之间快速切换来为 UIButton 设置动画?
You can use animationImages
property of your button's imageView
: 您可以使用按钮的imageView
animationImages
属性:
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. 编辑:由于@tidbeck指出,按钮需要具有分配的图像才能创建imageview
属性。
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. 我不知道这种效果是否足以满足您的需求。
This small example shows how to set a UIButton to show an indefinite animation of a heart beating.这个小例子展示了如何设置一个 UIButton 来显示一个无限期的心跳动画。
I have added 8 images into the Assets.xcassets, and named them like this: heart1<\/code> ,
heart2<\/code> , ...,
heart8<\/code> .
我在 Assets.xcassets 中添加了 8 张图片,并将它们命名为:
heart1<\/code> ,
heart2<\/code> , ...,
heart8<\/code> 。
private func showHeartedAnimation() {
var images = [UIImage]()
for i in 1...8 { images.append(UIImage(named: "heart\(i)")!) }
self.heartButton.setImage(UIImage.animatedImage(with: images, duration: 0.2), for: .normal)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.