我需要多次更改按钮的背景图像,我想使用图像序列来改变点击背景,但我找不到任何Swift文档。
也许有人知道该怎么做?
答案 0 :(得分:2)
这是其中一种方式。
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];
点击方法调用startAnimating方法。
答案 1 :(得分:2)
在斯威夫特:
var imageArray = [UIImage]()
for var index = 0; index < num; ++index {
imageArray.append( UIImage(named: "imageNumber\(index)" )! )
}
myButton.setImage( UIImage(named: "imageNumber1")! ), forState: .Normal)
myButton.imageView!.animationImages = imageArray
myButton.imageView!.animationDuration = 1.0
myButton.imageView!.startAnimating()