swift简单动画demo

前端之家收集整理的这篇文章主要介绍了swift简单动画demo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

from:
swift简单动画demo

动画GIF

No. name(cn) name(en) gif
1 缩放动画 Scale
2 位移动画 Translation
3 旋转动画 Rotation
4 颜色动画 Color
5 弹簧动画 Spring
6 组合动画 Concat

代码

  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5.  
  6.  
  7. var rect: UIView!
  8.  
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11.  
  12. initrectView()
  13. }
  14.  
  15. var i: Int = 0
  16.  
  17. @IBAction func onNextButtonClick(sender: UIButton) {
  18. switch i%6 {
  19. case 0:
  20. showScaleAnim() // 1. 缩放动画
  21. case 1:
  22. showTranslationAnim() // 2. 位移动画
  23. case 2:
  24. showRotationAnim() // 3. 旋转动画
  25. case 3:
  26. showColorAnim() // 4. 颜色动画
  27. case 4:
  28. showSpringAnim() // 5. 弹簧动画
  29. default:
  30. showConcatanim() // 6. 组合动画
  31. }
  32.  
  33. i += 1
  34. }
  35.  
  36.  
  37. /** 初始化动画对象 */
  38. func initrectView() {
  39. let rectFrame = CGRectMake(50,50,100,100)
  40. rect = UIView(frame: rectFrame)
  41.  
  42. rect.backgroundColor = UIColor.cyanColor()
  43.  
  44. self.view.addSubview(rect)
  45. }
  46.  
  47. func resumeRectView() -> Void {
  48. rect.transform = CGAffineTransformIdentity
  49. }
  50.  
  51. /** 普通动画. 缩放 */
  52. func showScaleAnim() {
  53.  
  54. let duration: NSTimeInterval = 3 // 动画持续时间
  55. let delay: NSTimeInterval = 0 // 动画延迟时间
  56. let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
  57. let animations: () -> Void = {() -> Void in
  58. self.rect.transform = CGAffineTransformMakeScale(2,2) // 缩放动画,x 放大 2,y 放大 2
  59. }
  60. UIView.animateWithDuration(duration,delay: delay,options: options,animations: animations) { (finish) -> Void in
  61. self.resumeRectView()
  62. print(finish)
  63. }
  64. }
  65.  
  66. /** 普通动画. 位移 */
  67. func showTranslationAnim() {
  68. let duration: NSTimeInterval = 3 // 动画持续时间
  69. let delay: NSTimeInterval = 0 // 动画延迟时间
  70. let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
  71. let animations: () -> Void = {() -> Void in
  72. self.rect.transform = CGAffineTransformMakeTranslation(0,100) // 位移动画,x 移动 0,y 移动 100
  73. }
  74. UIView.animateWithDuration(duration,animations: animations) { (finish) -> Void in
  75. self.resumeRectView()
  76. print(finish)
  77. }
  78. }
  79.  
  80. /** 普通动画. 旋转 */
  81. func showRotationAnim() {
  82. let duration: NSTimeInterval = 3 // 动画持续时间
  83. let delay: NSTimeInterval = 0 // 动画延迟时间
  84. let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
  85. let animations: () -> Void = {() -> Void in
  86. self.rect.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) // 旋转动画,旋转pi
  87. }
  88. UIView.animateWithDuration(duration,animations: animations) { (finish) -> Void in
  89. self.resumeRectView()
  90. print(finish)
  91. }
  92. }
  93.  
  94. /** 普通动画. 颜色 */
  95. func showColorAnim() {
  96. let duration: NSTimeInterval = 3 // 动画持续时间
  97. let delay: NSTimeInterval = 0 // 动画延迟时间
  98. let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
  99. let animations: () -> Void = {() -> Void in
  100. self.rect.backgroundColor = UIColor.greenColor() // 颜色动画,变色为绿色
  101. }
  102. UIView.animateWithDuration(duration,animations: animations) { (finish) -> Void in
  103. self.resumeRectView()
  104. print(finish)
  105. }
  106. }
  107.  
  108. /** 弹簧动画,一个球变大 */
  109. func showSpringAnim() {
  110. let duration: NSTimeInterval = 3 // 动画持续时间
  111. let delay: NSTimeInterval = 0 // 动画延迟时间
  112. let damping: CGFloat = 0.3 // 阻尼. 越大感觉越有粘性
  113. let velocity: CGFloat = 0 // 初速度
  114. let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
  115. let animations: () -> Void = {() -> Void in
  116. self.rect.transform = CGAffineTransformMakeTranslation(0,150) // 位移动画
  117. }
  118.  
  119. UIView.animateWithDuration(duration,usingSpringWithDamping: damping,initialSpringVelocity: velocity,animations: animations) { (finish) -> Void in
  120. self.resumeRectView()
  121. print(finish)
  122. }
  123. }
  124.  
  125. /** 组合动画. 缩放 + 位移 + 颜色 */
  126. func showConcatanim() {
  127. let duration: NSTimeInterval = 3 // 动画持续时间
  128. let delay: NSTimeInterval = 0 // 动画延迟时间
  129. let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
  130. let animations: () -> Void = {() -> Void in
  131.  
  132. let anim1 = CGAffineTransformMakeScale(2,y 放大 2
  133. let anim2 = CGAffineTransformMakeTranslation(150,50) // 位移动画,x 移动 150,y 移动 50
  134.  
  135. let concatanim = CGAffineTransformConcat(anim1,anim2) // 组合动画,缩放和位移动画一起组合成新的动画
  136.  
  137. self.rect.backgroundColor = UIColor.greenColor() // 颜色动画
  138.  
  139. self.rect.transform = concatanim
  140. }
  141. UIView.animateWithDuration(duration,animations: animations) { (finish) -> Void in
  142. self.resumeRectView()
  143. print(finish)
  144. }
  145. }
  146. }

猜你在找的Swift相关文章