我正试图在特定的持续时间内制作动画图像。它在Objective C中工作正常。但是,它不适用于Swift,我的错误在哪里?
Objective-C的代码是 -
-(void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *imgListArray=[NSMutableArray array];
for (int i=0; i<=11; i++)
{
NSString *strImageName=[NSString stringWithFormat:@"c%d.png", i];
NSLog(@"%@",strImageName);
UIImage *image=[UIImage imageNamed:strImageName];
[imgListArray addObject:image];
}
self.imgView.animationImages = imgListArray;
self.imgView.animationDuration =1.0f;
[self.imgView startAnimating];
// Do any additional setup after loading the view, typically from a nib
}
swift的代码是 -
override func viewDidLoad()
{
super.viewDidLoad()
var imgListArray :NSMutableArray = []
for countValue in 1...11
{
var strImageName : String = "c\(countValue).png"
var image = UIImage(named:strImageName) // suggested by Anil
imgListArray.addObject(image)
}
// Swift code HERE for Objective c
}
答案 0 :(得分:27)
[UIImage imageNamed (strImageName)]
这不是快速的代码。在swift中它会是
UIImage(named:strImageName)
修改后的代码:
var imgListArray :NSMutableArray = []
for countValue in 1...11
{
var strImageName : String = "c\(countValue).png"
var image = UIImage(named:strImageName)
imgListArray .addObject(image)
}
self.imageView.animationImages = imgListArray;
self.imageView.animationDuration = 1.0
self.imageView.startAnimating()
答案 1 :(得分:21)
对于Swift 2,请改用[UIImage]
。
var images: [UIImage] = []
for i in 1...2 {
images.append(UIImage(named: "c\(i)")!)
}
myImageView.animationImages = images
myImageView.animationDuration = 1.0
myImageView.startAnimating()
答案 2 :(得分:14)
在swift中,您可以更进一步,并有一个简单的行来执行此操作:
let loadingImages = (1...11).map { UIImage(named: "c\($0)")! }
然后你可以完成剩下的工作并将其注入imageView
self.imageView.animationImages = loadingImages
self.imageView.animationDuration = 1.0
self.imageView.startAnimating()
答案 3 :(得分:6)
在swift 3中 - 创建图像阵列并仅为其设置动画。
func animate_images()
{
let myimgArr = ["1.jpg","2.jpg","3.jpg"]
var images = [UIImage]()
for i in 0..<myimgArr.count
{
images.append(UIImage(named: myimgArr[i])!)
}
imgView_ref.animationImages = images
imgView_ref.animationDuration = 0.04
imgView_ref.animationRepeatCount = 2
imgView_ref.startAnimating()
}
对于停止动画,只需写
imgView_ref.stopAnimating()
答案 4 :(得分:1)
另一种方法,如果你想为图像数组设置动画:
var counter = 1
var timer = NSTimer()
@IBOutlet weak var someImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: Selector("doSomeAnimation"), userInfo: nil, repeats: true)
}
func doSomeAnimation() {
//I have four pngs in my project, which are named frame1.png ... and so on
if counter == 4 {
counter = 1
}else {
counter++
}
someImg.image = UIImage(named: "frame\(counter).png")
}
希望它有所帮助!!
答案 5 :(得分:1)
对于运行多个动画的任何人,根据变量,请尝试以下操作。
例如,您可以运行发送0或1的动画(可能基于您的应用知道其白天或夜晚):
func runAnimation (imgView: UIImageView, arrayPos: Int) {
let timingArray = [7.0,10.0] //Durations of each
let frameArray = [43,45] //Frames for each
var imgArray: Array<UIImage> = [] //Setup empty array
for i in 1 ... frameArray[arrayPos] {
//Fill empty array with images!!
let imageToAdd = UIImage(named: "animationNum_\(arrayPos)-frameNum_\(i)")
imgArray.append(imageToAdd!)
}
imgView.animationImages = imgArray
imgView.animationDuration = timingArray[arrayPos]
imgView.animationRepeatCount = 2
imgView.startAnimating()
}
答案 6 :(得分:1)
UIImageViews
可以通过两种不同的方式为图像制作动画(其他答案已经涵盖了深层的第一种方式):
[UIImage]
数组,其中包含要动画的图像animationImages
属性
animationDuration
属性animatedImage(with:duration:)
UIImage
中
image
属性以下代码使用#imageLiterals:
let images = [img1, img2, img3] // use of #imageLiterals here
let animation = UIImage.animatedImage(with: images, duration: 1)
self.myImageView.image = animation
<强>临强>
如果你必须经常更改UIImageView的图像,并且其中一个图像应该是动画的,那么你不必每次都开始/停止动画,你也不需要将animatedImages属性设置回nil
显示存储在图像视图的图像属性中的图像。
<强>缺点:强>
如果动画封装在单个UIImage
而不是数组中,则无法启动/停止动画。
更多关于#imageLiterals
:
如果您想使用图片文字,请键入imageLiteral
或只输入资源文件夹中的图片名称,Xcode的代码完成就会建议。
进一步阅读:
#imageLiterals
和#colorLiterals
制作动画的另一个好blog post。