这里的功能是为了动画观看如下: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
})
}
答案 0 :(得分:2)
您可以使用Core Animation代表永久重复您的动画,例如:
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
}
funcs的内容只是用于示例目的的代码,您可以在那里添加自定义动画。