Skip to content

Instantly share code, notes, and snippets.

@gawaooooo
Created March 29, 2015 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gawaooooo/4f08e5d29dc554e2f6f3 to your computer and use it in GitHub Desktop.
Save gawaooooo/4f08e5d29dc554e2f6f3 to your computer and use it in GitHub Desktop.
iOSで姓名診断アプリを作る swift版
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var myNavigaionController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// ViewControllerを生成
let myFirstViewController: ViewController = ViewController()
// Navigation Controllerを生成
myNavigaionController = UINavigationController(rootViewController: myFirstViewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
// Navigation ControllerをrootViewControllerに設定
self.window?.rootViewController = myNavigaionController
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:.
}
}
import UIKit
class ResultViewController: UIViewController {
// 入力された名前を入れる
var myName: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Controllerのタイトルを設定
self.title = "診断結果"
// Labelを作成
let label: UILabel = UILabel(frame: CGRectMake(0, 0, 200, 40))
// 背景色を付ける
label.backgroundColor = UIColor.lightGrayColor()
// 名前を代入
label.text = "\(myName)の点数は…"
// テキストを中央寄せにする
label.textAlignment = NSTextAlignment.Center
// ラベルを配置する座標を設定する
label.layer.position = CGPoint(x: self.view.bounds.width / 2, y: 200)
// ViewにLabelを追加
self.view.addSubview(label)
// 診断結果表示Labelを作成
let resultLabel: UILabel = UILabel(frame: CGRectMake(0, 0, 200, 80))
// 枠線を付ける
resultLabel.layer.borderColor = UIColor.redColor().CGColor
// 枠線の太さ
resultLabel.layer.borderWidth = 2.0
// 点数を表示
let score = arc4random_uniform(101)
resultLabel.text = "\(score)点"
// テキストを中央寄せ
resultLabel.textAlignment = NSTextAlignment.Center
// フォントサイズを大きくして太字に
resultLabel.font = UIFont.boldSystemFontOfSize(64)
// ラベルを配置する座標を設定する
resultLabel.layer.position = CGPoint(x: self.view.bounds.width / 2, y: 300)
// ViewにLabelを追加
self.view.addSubview(resultLabel)
}
/*
Viewが表示される直前に行う処理
*/
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// TODO viewの背景色を設定
// これで前の画面の残像(テキストフィールドとボタン)は消えたけど・・・??
self.view.backgroundColor = UIColor.whiteColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
let sendButton: UIButton = UIButton()
var nameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Controllerのタイトルを設定する
self.title = "姓名診断"
// UITextFieldを作成する
nameField = UITextField(frame: CGRectMake(0, 0, 300, 30))
// 最初に表示する文字を設定
nameField.placeholder = "名前を入力"
// Delegateを設定する
nameField.delegate = self
// 枠を表示する
nameField.borderStyle = UITextBorderStyle.RoundedRect
// UITextFieldの表示する位置を設定
nameField.layer.position = CGPoint(x: 160, y: 150)
// キーボードのReturnキーをSendに変える
nameField.returnKeyType = UIReturnKeyType.Send
// Viwに追加する
self.view.addSubview(nameField)
// 送信ボタンを設定する
sendButton.frame = CGRectMake(0, 0, 50, 40)
// 背景色を設定する
sendButton.backgroundColor = UIColor.lightGrayColor()
sendButton.showsTouchWhenHighlighted = true
// 枠を丸くする
sendButton.layer.masksToBounds = true
// タイトルを設定する
sendButton.setTitle("Send", forState: .Normal)
sendButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
// コーナーの半径を設定する
sendButton.layer.cornerRadius = 10.0
// ボタンの位置を指定する
sendButton.layer.position = CGPoint(x: 340, y: 150)
// タグを設定する
sendButton.tag = 1
// イベントを追加する
sendButton.addTarget(self, action: "showResult:", forControlEvents: .TouchUpInside)
// ボタンをViewに追加する
self.view.addSubview(sendButton)
}
/*
Viewが表示される直前に行う処理
*/
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// 名前入力テキストフィールドを空にする
nameField.text = ""
// 名前入力テキストフィールドにフォーカスする
nameField.becomeFirstResponder()
}
/*
ボタンイベント
*/
func showResult(sender: UIButton) {
// 空文字チェック
if nameField.text == "" {
showAlert()
} else {
// 移動先のViewを定義する
let resultViewController = ResultViewController()
// ResultViewに移動する
self.navigationController?.pushViewController(resultViewController, animated: true)
// self.navigationController?.pushViewController(resultViewController, animated: false)
// TODO これで渡せるな・・?
resultViewController.myName = nameField.text
}
}
/*
エラーアラートを表示
*/
func showAlert() {
// UIAlertControllerを作成する
let myAlert = UIAlertController(title: "エラー", message: "診断したい名前を入力してください", preferredStyle: .Alert)
// OKのアクションを作成する
let myOkAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
// OKのアクションを追加する
myAlert.addAction(myOkAction)
// UIAlertを発動する
presentViewController(myAlert, animated: true, completion: nil)
}
/*
改行ボタンが押された際に呼ばれる
*/
func textFieldShouldReturn(textField: UITextField) -> Bool {
// returnを押すとキーボードが隠れる(フォーカスをはずす)
textField.resignFirstResponder()
// sendButtonと同じ処理を
sendButton.sendActionsForControlEvents(.TouchUpInside)
return true
}
/*
UITextFieldが編集された直後に呼ばれる
*/
func textFieldDidBeginEditing(textField: UITextField) {
println("textFieldDidBeginEditing: \(textField.text)")
}
/*
UITextFieldが編集終了する直前に呼ばれる
*/
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
println("textFieldShouldEndEditing: \(textField.text)")
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment