iOS 上的动画API基于闭包,例如要实现一个持续3秒的背景渐变效果:
UIView.animateWithDuration(3){ () -> Void in self.view.backgroundColor = UIColor.grayColor() }
如果希望把许多不同的动画全部链接在一个特定的序列内,并在指定时刻进行,我们可以这样做:
UIView.animateKeyframesWithDuration(6,delay: 0,options: UIViewKeyframeAnimationOptions.LayoutSubviews,animations: { () -> Void in UIView.addKeyframeWithRelativeStartTime(0,relativeDuration: 3,animations: { () -> Void in self.view.backgroundColor = UIColor.magentaColor() }) UIView.addKeyframeWithRelativeStartTime(0,animations: { () -> Void in self.view.backgroundColor = UIColor.grayColor() }) }) { (Bool) -> Void in print("animation completed") }