Ios 在swift中重复链接动画
这里的函数是为了使视图动画化而编写的,如下所示:2秒淡出、2秒淡入、2秒延迟和重复。由于某些原因,动画预成型仅一次,不重复。我做错了什么Ios 在swift中重复链接动画,ios,iphone,swift,animation,Ios,Iphone,Swift,Animation,这里的函数是为了使视图动画化而编写的,如下所示:2秒淡出、2秒淡入、2秒延迟和重复。由于某些原因,动画预成型仅一次,不重复。我做错了什么 UIView.animateWithDuration(6.0, delay: 0.0, options: [.AllowUserInteraction,.Repeat,.BeginFromCurrentState], animations: { UIView.an
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
})
}
可以使用核心动画代理永久重复动画,例如:
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
}
函数的内容只是代码。例如,您可以在那里添加自定义动画。因为上一个
动画中的完成
块没有任何内容。你想要什么动画,你只需要调用self
?thanx作为解决方案。