我正在尝试使用设计有框架等多种图像的动画制作应用。我使用UIImage.animationImages,UIImages.animationDuration和UIImage.animationRepeatCount,但这仅显示在确定时间内具有指定图像列表的动画,但我需要在一个图像中停止很多秒,就像在图像列表的另一个图像中一样。例如,在Android中,我们可以选择制作xml设置图像和每个图像的持续时间,这就是我在Swift中所需要的
答案 0 :(得分:0)
例如,您可以像这样创建自定义UIImageView。
class CustomImageView : UIImageView
{
var animationDurations : [Float] = []
private var isAnimating = false
private var animationCount = 0
override func startAnimating() {
if let images = animationImages
{
if images.count == countElements(animationDurations)
{
animationCount = 0
isAnimating = true
self.changeImages(0)
}
}
}
override func stopAnimating() {
isAnimating = false
}
func changeImages(currentIndex : Int)
{
if currentIndex == 0 {
animationCount += 1
}
if animationCount > self.animationRepeatCount && self.animationRepeatCount > 0
{
isAnimating = false
}
if !isAnimating{
return
}
if let images = animationImages
{
let nextIndex = (currentIndex + 1) % images.count
self.image = images[currentIndex] as? UIImage
let floatDelay = animationDurations[currentIndex] * Float(NSEC_PER_SEC)
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(floatDelay))
dispatch_after(delay, dispatch_get_main_queue(), { () -> Void in
self.changeImages(nextIndex)
})
}
}
}
可以像
一样使用class ViewController: UIViewController {
var imageView : CustomImageView!
override func viewDidLoad() {
imageView = CustomImageView(frame: CGRectMake(100, 100, 60, 60))
self.view.addSubview(imageView)
imageView.animationRepeatCount = 2
let images : [UIImage] = [UIImage(named: "1.png")!,
UIImage(named: "2.png")!,
UIImage(named: "4.png")!]
let imageDuration : [Float] = [1.0,2.0,0.6]
imageView.animationImages = images
imageView.animationDurations = imageDuration
imageView.startAnimating()
}
}