LoginSignup
5
5

More than 5 years have passed since last update.

カウントアプリ

Last updated at Posted at 2016-06-20
1 / 14

※ スライドモードを試すために作成しています。

Xcode

  • 7.3.x
  • Swift 2.2

Playgorundを作成

Capture 2016-06-21 0.18.05.png


ソースコード


最低限必要な枠を作成

import UIKit
import XCPlayground // プレビュー用

// 表示用ViewController
class ViewController: UIViewController {

}

// プレビュー用に画面を設定
let currentPage = XCPlaygroundPage.currentPage
currentPage.liveView = ViewController()
currentPage.needsIndefiniteExecution = true

プロパティを設定

class ViewController: UIViewController {
    let label = UILabel()
    let button = UIButton(type: .System)

    var count: Int = 0    
}

画面の設定


画面の背景を設定

// var count... の下に追加
view.backgroundColor = .whiteColor() // 背景白
view.frame = CGRect(
    x: 0,
    y: 0,
    width: 320,
    height: 568
)

ラベルを追加

//     height: 568 
// ) の後に追加
label.text = "\(count)"
label.textAlignment = .Center
label.backgroundColor = .yellowColor()
label.frame = CGRect(
    x: 0,
    y: 0,
    width: 200,
    height: 100
)
view.addSubview(label)

ボタンを設定

// view.addSubview(label)の下に追加
button.setTitle("カウント", forState: .Normal)
button.addTarget(
    self,
    action: #selector(tapButton),
    forControlEvents: .TouchUpInside
)
button.frame = CGRect(
    x: 0,
    y: 0,
    width: 100,
    height: 50
)
view.addSubview(button)

ボタンを押したときの動作を追加

//     view.addSubview(button)
// } の下に追加
func tapButton() {
    count += 1
    label.text = "\(count)"
}

ラベルとボタンをセンターに配置

//     label.text = "\(count)"
// } の下に追加
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    label.center = view.center
    button.center = CGPoint(x: view.center.x, y: view.center.y + 100)
}

完成


image

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5