今日の写経14
今日の写経
015 UIViewControllerで画面遷移する - Swift Docs
所感
特になし。 面白いコードでもない。
画面キャプチャ
ソースコード
// AppDelegate.swift
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { private var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // ViewControllerを生成する let myFirstViewController: FirstViewController = FirstViewController() // UIWindowを生成する self.window = UIWindow(frame: UIScreen.mainScreen().bounds) // rootCiewControllerにNatigationControllerを設定する self.window?.rootViewController = myFirstViewController self.window?.makeKeyAndVisible() return true } func applicationWillResignActive(application: UIApplication) { // 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. // 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. } func applicationDidEnterBackground(application: UIApplication) { // 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. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) { // 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. } func applicationDidBecomeActive(application: UIApplication) { // 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. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }
// FirstVireController.swift
import UIKit class FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 背景色をGreenに設定する self.view.backgroundColor = UIColor.greenColor() // ボタンを生成する let nextButton: UIButton = UIButton(frame: CGRectMake(0, 0, 120, 50)) nextButton.backgroundColor = UIColor.redColor() nextButton.layer.masksToBounds = true nextButton.setTitle("Next", forState: .Normal) nextButton.layer.cornerRadius = 20.0 nextButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height - 50) nextButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) // ボタンを追加する self.view.addSubview(nextButton) } /* ボタンイベント */ internal func onClickMyButton(sender: UIButton) { // 遷移するViewを定義する let mySecondViewController: UIViewController = SecondViewController() // アニメーションを設定する mySecondViewController.modalTransitionStyle = UIModalTransitionStyle.PartialCurl // Viewの移動 self.presentViewController(mySecondViewController, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
// SecondViewController.swift
import UIKit class SecondViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 背景色を設定 self.view.backgroundColor = UIColor.blueColor() // ボタンを作成 let backButton: UIButton = UIButton(frame: CGRectMake(0, 0, 120, 50)) backButton.backgroundColor = UIColor.redColor() backButton.layer.masksToBounds = true backButton.setTitle("Back", forState: .Normal) backButton.layer.cornerRadius = 20.0 backButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height - 50) backButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) self.view.addSubview(backButton) } /* ボタンイベント */ internal func onClickMyButton(sender: UIButton) { // 遷移するViewを定義 let myViewController: UIViewController = FirstViewController() //アニメーションを設定 myViewController.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal // viewの移動 self.presentViewController(myViewController, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }