swift 动画切换View心地层次顺序

来源:互联网 发布:fetch js 跨域 编辑:程序博客网 时间:2024/06/05 17:04

1.动画效果




2.代码

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  AppDelegate.swift  
  3. //  AnimationDemo  
  4. //  
  5. //  Created by 赵超 on 14-6-27.  
  6. //  Copyright (c) 2014年 赵超. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. @UIApplicationMain  
  12. class AppDelegate: UIResponder, UIApplicationDelegate {  
  13.                               
  14.     var window: UIWindow?  
  15.     //响应点击事件  
  16.     func onclick(){  
  17.         //开始动画  
  18.         UIView.beginAnimations("test",context: nil)  
  19.         //动画时长  
  20.         UIView.setAnimationDuration(1)  
  21.         // FlipFromRight  
  22.         // CurlUp  
  23.         //动画样式  
  24.         UIView.setAnimationTransition(UIViewAnimationTransition.CurlUp,  
  25.             forView :self.window!,  
  26.             cache:true)  
  27.         //更改view顺序  
  28.         self.window!.exchangeSubviewAtIndex(0,withSubviewAtIndex :1)  
  29.         //提交动画  
  30.         UIView.commitAnimations()  
  31.   
  32.     }  
  33.     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {  
  34.         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  35.         // Override point for customization after application launch.  
  36.         self.window!.backgroundColor = UIColor.whiteColor()  
  37.         self.window!.makeKeyAndVisible()  
  38.           
  39.         //第一个view  
  40.         var vc1=UIView(frame:UIScreen.mainScreen().applicationFrame)  
  41.         vc1.backgroundColor=UIColor.grayColor()  
  42.         //第二个view  
  43.         var vc2 = UIView(frame:UIScreen.mainScreen().applicationFrame)  
  44.         vc2.backgroundColor=UIColor.blueColor()  
  45.         //添加点击按钮  
  46.         var btn=UIButton(frame:CGRectMake(40,40,200,40))  
  47.         btn.backgroundColor=UIColor.redColor()  
  48.         btn.setTitle("点击切换View",forState: .Normal)  
  49.         btn.addTarget(self,action:"onclick",forControlEvents : UIControlEvents.TouchUpInside)  
  50.           
  51.         self.window!.addSubview(btn)  
  52.         self.window!.addSubview(vc1)  
  53.         self.window!.addSubview(vc2)  
  54.         //将按钮放到顶层  
  55.         self.window!.bringSubviewToFront(btn)  
  56.           
  57.           
  58.         return true  
  59.     }  
  60.   
  61.     func applicationWillResignActive(application: UIApplication) {  
  62.         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.  
  63.         // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.  
  64.     }  
  65.   
  66.     func applicationDidEnterBackground(application: UIApplication) {  
  67.         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.  
  68.         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  69.     }  
  70.   
  71.     func applicationWillEnterForeground(application: UIApplication) {  
  72.         // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.  
  73.     }  
  74.   
  75.     func applicationDidBecomeActive(application: UIApplication) {  
  76.         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.  
  77.     }  
  78.   
  79.     func applicationWillTerminate(application: UIApplication) {  
  80.         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  81.     }  
  82.   
  83.   
  84. }  
0 0
原创粉丝点击