Skip to content

Instantly share code, notes, and snippets.

@gawaooooo
Last active August 29, 2015 14:17
Show Gist options
  • Save gawaooooo/756c9ecb658687867171 to your computer and use it in GitHub Desktop.
Save gawaooooo/756c9ecb658687867171 to your computer and use it in GitHub Desktop.
iOSでおみくじを作る swift版
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
self.window?.rootViewController = ViewController()
self.window?.makeKeyAndVisible()
return true
}
import UIKit
class ViewController: UIViewController {
// 占うボタン
let divineButton: UIButton = UIButton()
// 結果ラベル
let resultLabel: UILabel = UILabel()
// おみくじの結果
enum Omikuji: Int {
case Daikichi = 0
case Kichi
case Cyukichi
case Syokichi
case Kyo
case DaiKyo
// ラベルに表示する文字列と文字色を返す
func getStatus() -> (String, UIColor) {
switch self {
case .Daikichi:
return ("大吉", UIColor.redColor())
case .Kichi:
return ("吉", UIColor.orangeColor())
case .Cyukichi:
return ("中吉", UIColor.greenColor())
case .Syokichi:
return ("小吉", UIColor.grayColor())
case .Kyo:
return ("凶", UIColor.lightGrayColor())
case .DaiKyo:
return ("大凶", UIColor.blueColor())
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
/*
タイトルラベル
*/
// ラベルを作成
let titleLabel: UILabel = UILabel(frame: CGRectMake(0, 0, 200, 50))
// わかりやすいように背景色を付ける
titleLabel.backgroundColor = UIColor.yellowColor()
// 文字を代入
titleLabel.text = "あなたの運勢は..."
// テキストを中央寄せにする
titleLabel.textAlignment = NSTextAlignment.Center
// ラベルを配置する座標を設定する
titleLabel.layer.position = CGPoint(x: self.view.bounds.width / 2, y: 200)
// ViewにLabelを追加
self.view.addSubview(titleLabel)
/*
占い結果ラベル
*/
// ラベルを作成
resultLabel.frame = CGRectMake(0, 0, 200, 80)
// ラベルに枠線を付ける
// 枠線の色
resultLabel.layer.borderColor = UIColor.blackColor().CGColor
// 枠線の太さ
resultLabel.layer.borderWidth = 2.0
// コーナーの半径
resultLabel.layer.cornerRadius = 30
// 文字を代入
resultLabel.text = "????"
// テキストを中央寄せにする
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)
/*
占うボタン
*/
// サイズを設定
divineButton.frame = CGRectMake(0, 0, 200, 40)
// 背景色
divineButton.backgroundColor = UIColor.redColor()
// 枠を丸くする
divineButton.layer.masksToBounds = true
// タイトルを設定する(通常時)
divineButton.setTitle("運勢を占う", forState: UIControlState.Normal)
divineButton.setTitleColor(UIColor.purpleColor(), forState: UIControlState.Normal)
// タイトルを設定する(ボタンがハイライトされた時)
divineButton.setTitle("占い中!", forState: UIControlState.Highlighted)
divineButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
// コーナーの半径を設定する
divineButton.layer.cornerRadius = 10.0
// ボタンの位置を指定する
divineButton.layer.position = CGPoint(x: self.view.frame.width / 2, y: 400)
// タグを設定する
divineButton.tag = 1
// イベントを追加する
divineButton.addTarget(self, action: "onClickDivineButton:", forControlEvents: .TouchUpInside)
// ボタンをViewに追加する
self.view.addSubview(divineButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
占うボタンを押した時の処理
*/
func onClickDivineButton(sender: UIButton) {
// 乱数を生成 0 - 5(0 - n)
// arc4random_uniform(n + 1)
let random = Int(arc4random_uniform(UInt32(Omikuji.DaiKyo.rawValue + 1)))
if let r = Omikuji(rawValue: random) {
// 結果を取得
let (msg, color) = r.getStatus()
// 結果を表示
resultLabel.textColor = color
resultLabel.text = msg
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment