from:
swift简单动画demo
No. | name(cn) | name(en) | gif |
---|---|---|---|
1 | 缩放动画 | Scale | |
2 | 位移动画 | Translation | |
3 | 旋转动画 | Rotation | |
4 | 颜色动画 | color | |
5 | d簧动画 | Spring | |
6 | 组合动画 | Concat |
import UIKitclass VIEwController: UIVIEwController { var rect: UIVIEw! overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() initrectVIEw() } var i: Int = 0 @IBAction func onNextbuttonClick(sender: UIbutton) { switch i%6 { case 0: showScaleAnim() // 1. 缩放动画 case 1: showTranslationAnim() // 2. 位移动画 case 2: showRotationAnim() // 3. 旋转动画 case 3: showcolorAnim() // 4. 颜色动画 case 4: showSpringAnim() // 5. d簧动画 default: showConcatanim() // 6. 组合动画 } i += 1 } /** 初始化动画对象 */ func initrectVIEw() { let rectFrame = CGRectMake(50,50,100,100) rect = UIVIEw(frame: rectFrame) rect.backgroundcolor = UIcolor.cyancolor() self.vIEw.addSubvIEw(rect) } func resumeRectVIEw() -> VoID { rect.transform = CGAffinetransformIDentity } /** 普通动画. 缩放 */ func showScaleAnim() { let duration: NSTimeInterval = 3 // 动画持续时间 let delay: NSTimeInterval = 0 // 动画延迟时间 let options: UIVIEwAnimationoptions = UIVIEwAnimationoptions.CurveEaseInOut // 动画选项 let animations: () -> VoID = {() -> VoID in self.rect.transform = CGAffinetransformMakeScale(2,2) // 缩放动画,x 放大 2,y 放大 2 } UIVIEw.animateWithDuration(duration,delay: delay,options: options,animations: animations) { (finish) -> VoID in self.resumeRectVIEw() print(finish) } } /** 普通动画. 位移 */ func showTranslationAnim() { let duration: NSTimeInterval = 3 // 动画持续时间 let delay: NSTimeInterval = 0 // 动画延迟时间 let options: UIVIEwAnimationoptions = UIVIEwAnimationoptions.CurveEaseInOut // 动画选项 let animations: () -> VoID = {() -> VoID in self.rect.transform = CGAffinetransformMakeTranslation(0,100) // 位移动画,x 移动 0,y 移动 100 } UIVIEw.animateWithDuration(duration,animations: animations) { (finish) -> VoID in self.resumeRectVIEw() print(finish) } } /** 普通动画. 旋转 */ func showRotationAnim() { let duration: NSTimeInterval = 3 // 动画持续时间 let delay: NSTimeInterval = 0 // 动画延迟时间 let options: UIVIEwAnimationoptions = UIVIEwAnimationoptions.CurveEaseInOut // 动画选项 let animations: () -> VoID = {() -> VoID in self.rect.transform = CGAffinetransformMakeRotation(CGfloat(M_PI_2)) // 旋转动画,旋转pi } UIVIEw.animateWithDuration(duration,animations: animations) { (finish) -> VoID in self.resumeRectVIEw() print(finish) } } /** 普通动画. 颜色 */ func showcolorAnim() { let duration: NSTimeInterval = 3 // 动画持续时间 let delay: NSTimeInterval = 0 // 动画延迟时间 let options: UIVIEwAnimationoptions = UIVIEwAnimationoptions.CurveEaseInOut // 动画选项 let animations: () -> VoID = {() -> VoID in self.rect.backgroundcolor = UIcolor.greencolor() // 颜色动画,变色为绿色 } UIVIEw.animateWithDuration(duration,animations: animations) { (finish) -> VoID in self.resumeRectVIEw() print(finish) } } /** d簧动画,一个球变大 */ func showSpringAnim() { let duration: NSTimeInterval = 3 // 动画持续时间 let delay: NSTimeInterval = 0 // 动画延迟时间 let damPing: CGfloat = 0.3 // 阻尼. 越大感觉越有粘性 let veLocity: CGfloat = 0 // 初速度 let options: UIVIEwAnimationoptions = UIVIEwAnimationoptions.CurveEaseInOut // 动画选项 let animations: () -> VoID = {() -> VoID in self.rect.transform = CGAffinetransformMakeTranslation(0,150) // 位移动画 } UIVIEw.animateWithDuration(duration,usingSpringWithdamPing: damPing,initialSpringVeLocity: veLocity,animations: animations) { (finish) -> VoID in self.resumeRectVIEw() print(finish) } } /** 组合动画. 缩放 + 位移 + 颜色 */ func showConcatanim() { let duration: NSTimeInterval = 3 // 动画持续时间 let delay: NSTimeInterval = 0 // 动画延迟时间 let options: UIVIEwAnimationoptions = UIVIEwAnimationoptions.CurveEaseInOut // 动画选项 let animations: () -> VoID = {() -> VoID in let anim1 = CGAffinetransformMakeScale(2,y 放大 2 let anim2 = CGAffinetransformMakeTranslation(150,50) // 位移动画,x 移动 150,y 移动 50 let concatanim = CGAffinetransformConcat(anim1,anim2) // 组合动画,缩放和位移动画一起组合成新的动画 self.rect.backgroundcolor = UIcolor.greencolor() // 颜色动画 self.rect.transform = concatanim } UIVIEw.animateWithDuration(duration,animations: animations) { (finish) -> VoID in self.resumeRectVIEw() print(finish) } }}总结
以上是内存溢出为你收集整理的swift简单动画demo全部内容,希望文章能够帮你解决swift简单动画demo所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)