当アカウントへ訪れていただき、誠にありがとうございます。第22回アプリ教室では、時計アプリを作ります。自分のペースで勉強したい、勉強前に予習したい、内容を復習したい場合、ご利用ください。
Meetup http://www.meetup.com/ios-dev-in-namba/
http://learning-ios-dev.esy.es/
株式会社ジーライブ http://geelive-inc.com
Xcode 9.1 / Swift 4
0-1. Xcodeを起動。
0-2. "Create a new Xcode project"を選択。
0-3. "Single View Application"を選択して"Next"をクリック。
0-4. "Product name"を適当に入力して"Next"をクリック。
0-5. プロジェクトの場所を指定して"Create"をクリック。
1-1. 画像素材を"Assets.xcassets"にドラッグ&ドロップ。
2-1. UIImageView を storyboad に追加
2-2. 画像を設定し、大きさを調整
2-3. 時間を表示するための UILabel を storyboad に追加
背景画像に合わせてテキストカラーを調整
2-4. UILabel の 文字サイズ等を調整
3-1. UILabel を "ViewController.swift" に紐付ける
★ controlを押しながらドラッグ
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var timeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let timer = Timer.scheduledTimer(
timeInterval: 1.0,
target: self,
selector: #selector(updateTimer),
userInfo: nil,
repeats: true
)
timer.fire()
}
@objc func updateTimer() {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
// Set current time
timeLabel.text = formatter.string(from: Date())
}
}
5-1. 日付を表示するラベルも追加してみてください。
★ 参考
formatter.dateFormat = "yyyy/MM/dd" // 2017/01/01