ios - swift 重复链式动画

标签 ios iphone swift animation

这里的函数是为了使 View 动画如下:2 秒淡出,2 秒淡入,2 秒延迟和重复。由于某种原因,动画只预制一次并且不重复。我在这里做错了什么?

UIView.animateWithDuration(6.0,
            delay: 0.0,
            options: [.AllowUserInteraction,.Repeat,.BeginFromCurrentState],
            animations: {

            UIView.animateWithDuration(2.0,
                delay: 0.0,
                options: [.AllowUserInteraction,.BeginFromCurrentState] ,
                animations: {
                    //fade out
                    self.alpha = 0.5
                },
                completion: { finished in

                    UIView.animateWithDuration(2.0,
                        delay: 0.0,
                        options: [.AllowUserInteraction,.BeginFromCurrentState],
                        animations: {
                            //fade in

                            self.alpha = 1.0
                        },
                        completion: { finished in

                            UIView.animateWithDuration(2.0,
                                delay: 0.0,
                                options: [.AllowUserInteraction,.BeginFromCurrentState],
                                animations: {

                                },
                                completion: { finished in                                    
                            })                                                             
                   })                              
            })
        },
        completion: { finished in
    })
}

最佳答案

您可以使用 Core Animation delegates 来永远重复您的动画,例如:

func animateCustomView(layer: CALayer) {
    let speed = 60.0 / Double(view.layer.frame.size.width)
    let duration: NSTimeInterval = Double(view.layer.frame.size.width - layer.frame.origin.x) * speed

    let move = CABasicAnimation(keyPath: "position.x")
    move.duration = duration
    move.toValue = self.view.bounds.size.width + layer.bounds.width / 2
    move.delegate = self
    move.setValue("view", forKey: "name")
    move.setValue(layer, forKey: "layer")

    layer.addAnimation(move, forKey: nil)
}

// Core Animation Delegate implementation

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {

    if let name = anim.valueForKey("name") as? String{
        if name == "view" {
            let layer = anim.valueForKey("layer") as? CALayer
            layer?.position.x = -(layer?.bounds.width)! / 2
            delay(seconds: 0.5, completion: { 
                self.animateCustomView(layer!)
            })
            // Here add more delays and in completion handler block, add your chained animations.
        } else if name == "other name" {
            // or you can reinitialize it your another chained animations, delay it and call it again.
        }
    }
}

override func viewDidAppear(animated: Bool) {
    animateCustomView(viewToAnimate.layer)
    // Here call your other chained animations
}

函数的内容只是用于示例目的的代码,您可以在那里添加自定义动画。

关于ios - swift 重复链式动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36841514/

相关文章:

ios - 在关闭之前的 AlertViewController 之后呈现新的 AlertViewController - Swift

iOS viewController 和DetailViewController 流程​​/设计

iphone - 多个复选框选择值显示到 TableView 的不同单元格中

javascript - 在 iPad 上出现两次的标签

ios - Xcode 7 上的 IOHIDLib 和更多奇怪的错误

ios - 测试迁移映射模型的核心数据

iphone - 创建一个包含许多产品的数据库

iphone - RedLaser 设置事件区域 iPhone

ios - 当我不注销就终止应用程序时,经过身份验证的用户是否保持登录状态?

iphone - 如何使用 Alamofire SWIFT 在 post api 中发送 JSON