我有一个脉冲动画,我想运行3或4秒,我正在寻找一种方法来延迟和运行该动画一段时间,然后切换到下一个屏幕.我使用下面的代码来表示延迟,但是当我调用该函数时它不起作用.
NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(3), target: self, selector: "functionHere", userInfo: nil, repeats: false)
Run Code Online (Sandbox Code Playgroud)
如果您有任何想法,请告诉我.
let pulseAnimation = CABasicAnimation(keyPath: "opacity")
pulseAnimation.duration = 1
pulseAnimation.fromValue = 0
pulseAnimation.toValue = 1
pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = FLT_MAX
XboxOneL.layer.addAnimation(pulseAnimation, forKey: "animateOpacity")
XboxOneR.layer.addAnimation(pulseAnimation, forKey: "animateOpacity")
Run Code Online (Sandbox Code Playgroud)
根据动画的实现方式,可以使用:
UIView.animate(withDuration: 0.3, delay: 5.0, options: [], animations: {
//Animations
}) { (finished) in
//Perform segue
}
Run Code Online (Sandbox Code Playgroud)
这将在动画运行之前施加延迟,然后运行完成块(在其中执行segue)。
编辑:您更新了问题,以使用CAAnimations,所以我认为此解决方案不起作用。
利用 UIView.animate(withDuration:) 的完成处理程序。下面的基本示例:
func performMyAnimation() {
UIView.animate(withDuration: {$duration},
animations: { /* Animation Here */ },
completion: { _ in self.performSegue(withIdentifier: "segueIdentifierHere", sender: nil) }
}
Run Code Online (Sandbox Code Playgroud)
您的动画在animations块中运行。完成后,您将执行转场到下一个屏幕。
编辑:由于问题现在包括动画代码,使用CABasicAnimation,那么这个CAAnimation 回调的答案可能对您有用。
基本上,您需要CABasicAnimation用CATransaction.
CATransaction.begin()
CATransaction.setCompletionBlock({
self.performSegue(withIdentifier: "segueIdentifierHere", sender: nil)
})
// Your animation here
CATransaction.commit()
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找Swift 3中标准的延迟,那么最好将DispatchQueue API与UIView.animate(withDuration:_:).一起使用.
例如,如果你想延迟1.0秒动画3秒:
let yourDelay = 3
let yourDuration = 1.0
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(yourDelay), execute: { () -> Void in
UIView.animate(withDuration: yourDuration, animations: { () -> Void in
// your animation logic here
})
})
Run Code Online (Sandbox Code Playgroud)
如果这对您有意义,请告诉我
| 归档时间: |
|
| 查看次数: |
8656 次 |
| 最近记录: |