我正在实现一个自定义的CATransition,所以我可以从左边或右边而不是底部呈现ViewController,但是遇到了一个有趣的问题,即CATransition做了什么。
这是我的代码:
let transition:CATransition = CATransition()
transition.duration = 2
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
transition.type = kCATransitionMoveIn
transition.subtype = kCATransitionFromLeft
self.view.window?.layer.addAnimation(transition, forKey: nil)
正在呈现的视图中,所有内容都按预期工作,但正在替换的视图中发生的事情是有趣的。它会逐渐消失为黑色,这会在我的新视图完全动画化之前产生令人讨厌的黑色闪光。
如下图所示,我的新视图越接近完全动画,旧视图就越暗。在视图完全转换之前,它会达到全黑。
答案 0 :(得分:1)
您可以直接转接view.layer并更改窗口的颜色。
@IBAction func test(_ sender: Any){
let transition:CATransition = CATransition()
transition.duration = 2
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.default)
transition.type = CATransitionType.moveIn
transition.subtype = CATransitionSubtype.fromLeft
self.view.window?.backgroundColor = UIColor.red
self.view.layer.add(transition, forKey: nil)
}
如果您希望颜色不闪烁,只需将两个目标颜色设置为相同的值:
let transition:CATransition = CATransition()
transition.duration = 2
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.default)
transition.type = CATransitionType.moveIn
transition.subtype = CATransitionSubtype.fromLeft
self.view.window?.backgroundColor = UIColor.green
self.view.layer.backgroundColor = UIColor.green.cgColor
self.view.layer.add(transition, forKey: nil)
}
您甚至可以添加自定义动画来获得所需的颜色过渡。
@IBAction func test(_ sender: Any){
let transition:CATransition = CATransition()
transition.duration = 2
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.default)
transition.type = CATransitionType.moveIn
transition.subtype = CATransitionSubtype.fromLeft
let colorAnimation = CABasicAnimation.init(keyPath: "backgroundColor")
colorAnimation.duration = 1.5
colorAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.default)
colorAnimation.toValue = UIColor.black.cgColor
colorAnimation.fromValue = UIColor.red.cgColor
self.view.window?.backgroundColor = UIColor.red
self.view.layer.backgroundColor = UIColor.white.cgColor
self.view.window?.layer.add(colorAnimation , forKey: nil)
self.view.layer.add(transition, forKey: nil)
}
答案 1 :(得分:0)
由于 CATransition 的默认功能,因此无法使用。您需要更改窗口的颜色以避免这种情况。