gpt4 book ai didi

ios - swift 重复链式动画

转载 作者:搜寻专家 更新时间:2023-11-01 07:24:45 24 4
gpt4 key购买 nike

这里的函数是为了使 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com