本文是学习慕课网UIView动画的笔记,可用于简单的创建动画,
慕课网视频链接如下:
http://www.imooc.com/learn/392
通过使用动画,我们可以使我们的iOS App更加生动,提高用户体验和粘性。
通常,我们的动画是在页面刚刚显示(viewDidAppear)或者点击了某个按键(Action)的时候开始,持续一小段时间结束
使用的API是:
Void UIView.animateWithDuration(duration:NSTimeInterval, animations: () -> Void)
当然还有很多变体的方法,当我们在Xcode输入函数名的时候,都可以展现出来,包括延迟执行和执行完成后,还可以调用另一个closure
我们的比较初级的动画效果分为如下几类:
- 位置
- 透明度
- 大小
- 颜色
- 翻转
下面我们依次进行说明:
位置
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
UIView.animateWithDuration(1, animations:{
self.blueSquare.center.x = self.view.bounds.width - self.blueSquare.center.x
})
UIView.animateWithDuration(1, delay:0.5, options: nil, animations:{
self.redSquare.center.y = self.view.bounds.height - self.redSquare.center.y
}, completion: nil)
UIView.animateWithDuration(1, delay:0.5, options: nil, animations:{
self.greenSquare.center.x = self.view.bounds.width - self.greenSquare.center.x
self.greenSquare.center.y = self.view.bounds.height - self.greenSquare.center.y
}, completion: nil)
}
透明度(Opacity)
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
UIView.animateWithDuration(1, animations:{
self.blueSquare.alpha = 0.1
})
}
大小
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
UIView.animateWithDuration(1, animations:{
self.blueSquare.transform = CGAffineTransformMakeScale(2.0,2.0)
})
}
颜色
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
UIView.animateWithDuration(1, animations:{
self.blueSquare.backgroundColor = UIColor.redColor()
self.label.textColor = UIColor.whiteColor()
})
}
翻转
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
UIView.animateWithDuration(1, animations:{
self.blueSqure.transform = CGAffineTransformRotation(CGFloat(M_PI))
})
}