【问题标题】:Animations with SwiftSwift 动画
【发布时间】:2015-01-31 17:59:30
【问题描述】:

我正在尝试制作一个应用程序,其动画设计有多个图像,如框架。我使用 UIImage.animationImages、UIImages.animationDuration 和 UIImage.animationRepeatCount,但这仅在确定的时间内显示具有指定图像列表的动画,但我需要在一个图像中停止很多秒,就像在图像列表的另一个图像中一样。例如,在 Android 中,我们可以选择进行 xml 设置图像和每张图像的持续时间,这就是我在 Swift 中所需要的

【问题讨论】:

    标签: animation swift


    【解决方案1】:

    例如,您可以像这样创建自定义 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()
    
        }
    }
    

    【讨论】:

    • 我尝试这些使外部类重用,但是当我尝试在模拟器中运行时,它返回错误:“线程 1:EXC_BAD_ACCESS (Code=2,...)”: imageView.animationDurations = imageDuration
    • 您是否将其添加为 IBoutlet 并使用界面构建器?
    • 是的,您的代码和我的代码之间的唯一变化是我的 UIView 是来自情节提要的 IBOutlet 而不是硬编码
    • 你是否在界面生成器中将类设置为customimageview
    • 你必须设置你放置在界面生成器中的uiimageview中的类
    猜你喜欢
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多