13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift2.0] UILabelとUIImageViewのタップイベント処理を実装する

Last updated at Posted at 2015-10-06

tagを使ってタップイベントを処理します。

実装例(Swift2.0)

class MainViewController: UIViewController {
  @IBOutlet weak var sampleImageView: UIImageView!
  @IBOutlet weak var sampleNameLabel: UILabel!
  
  let tagSampleImageView = 1
  let tagSampleNameLabel = 2
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    sampleImageView.userInteractionEnabled = true
    sampleImageView.tag = tagSampleImageView
    
    sampleNameLabel.userInteractionEnabled = true
    sampleNameLabel.tag = tagSampleNameLabel
  }

  override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    super.touchesEnded(touches, withEvent: event)
    for touch: UITouch in touches {
      let tag = touch.view!.tag
      switch tag {
      case tagSampleImageView, tagSampleNameLabel:
        print("tapped")
      default:
        break
      }
    }
  }
}
13
10
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
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?