Created
November 19, 2016 03:36
-
-
Save takoikatakotako/0fb9d9197ce5817e365b064adbd2b409 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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