我正在关注this教程。代码片段可以找到here。在一些动画中,动画被直接发送到UIView
。像下面的代码片段一样。创建动画时,为什么要向UIView类属性发送消息?
问题1:这个UIView是什么类的属性?或者,我们是否在屏幕上的所有内容的UIView上发送消息,以完成代码块中的内容?我只是想了解我们在这里短信哪个属性...
protocol Flashable {}
extension Flashable where Self: UIView {
func flash() {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.alpha = 1.0
}) { (animationComplete) in
if animationComplete == true {
UIView.animate(withDuration: 0.3, delay: 2.0, options: .curveEaseOut, animations: {
self.alpha = 0.0
}, completion: nil)
}
}
}
}
^^^我不明白UIView.animate
一部分。 ^^^
而对于一些它不直接发送到UIView
。
extension Jitterable where Self: UIView {
func jitter() {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.05
animation.repeatCount = 5
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint.init(x: self.center.x - 5.0, y: self.center.y))
animation.toValue = NSValue(cgPoint: CGPoint.init(x: self.center.x + 5.0, y: self.center.y))
layer.add(animation, forKey: "position")
}
}
^^^上面的代码更容易理解^^^
但是当你想打电话给他们都这样做:
errorLabel.flash()
errorLabel.jitter()
问题2:有什么之间的区别这两种动画是如何写的?
Thanks 1.所以理论上我们可以在'ViewController'类上编写全局函数,但是对于命名的目的,它可能不是最好的选择吗? 2. *创建隐式全局CATransaction以包装所有当前动画* < - 包装整个应用程序中的所有动画(尽管我们通常每次运行一个动画片段,因为它耗费CPU)? – Honey
我相信你可以做一个关于将它附加到UIViewController的一个体面的论点 - 毕竟有些人可能认为动画是MVC中“C”的一部分。但事实上,任何可以生成动画的东西都是UIView或它的一个子类,我认为动画是MVC中“V”的一部分。作为Swift 3的一部分,已经有了一个强有力的举措,即更加“面向行动”的事物命名。也许可以用这些术语来思考,在Swift出现之前很久,你就可以看到他们把它放在正确的地方。 :-) – dfd
你能回答我评论的第二个问题吗? – Honey