Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created November 19, 2016 03:36
Show Gist options
  • Save takoikatakotako/0fb9d9197ce5817e365b064adbd2b409 to your computer and use it in GitHub Desktop.
Save takoikatakotako/0fb9d9197ce5817e365b064adbd2b409 to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController {
let tapLabel = UILabel()
let tapLabelTag = 1
override func viewDidLoad() {
super.viewDidLoad()
tapLabel.frame = CGRect(x: 50, y: 200, width: 300, height: 80)
tapLabel.textColor = UIColor.red //文字色を赤色に設定
tapLabel.backgroundColor = UIColor.lightGray //背景色を灰色に設定
tapLabel.textAlignment = NSTextAlignment.center //センター揃え
tapLabel.text = "押されたら反応するLabel"
tapLabel.isUserInteractionEnabled = true //タッチイベントを有効化
tapLabel.tag = tapLabelTag
self.view.addSubview(tapLabel)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: UITouch in touches {
let tag = touch.view!.tag
switch tag {
case tapLabelTag:
print("tapLabelがタップされました")
default:
print("その他がタップされました")
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment