我有两个PNG图片,希望将它们作为按钮使用。如何通过快速地在这两张图片之间切换来实现对UIButton的动画效果?
我有两个PNG图片,希望将它们作为按钮使用。如何通过快速地在这两张图片之间切换来实现对UIButton的动画效果?
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];
你的按钮将在两张图片之间切换。
编辑:正如@tidbeck指出的那样,需要为按钮分配一张图像才能创建imageview
属性。
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)
}
ImageView
,我需要先在按钮上设置一张图片:[self.button setImage:[UIImage imageNamed:"img.png"] forState:UIControlStateNormal];
。 - tidbeck[myButton setImage:[UIImage animatedImageNamed:@"image" duration:0.5] forState:UIControlStateNormal]
。在Swift中,您可以使用以下代码:myButton.setImage(UIImage.animatedImageNamed("image", duration: 0.5), forState: .Normal)
。 - awph