3
4

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.

[Swift] [iOS] switch文で二次元の範囲を指定したいときの書き方

3
Posted at

目的

よくタップの場所の判定を二次元で考えたい時にswitch文を使うのでメモ
switch文に直接範囲を書くと長くなるのでClosedInterval<CGFloat>を使う。

書き方

let pictureXRange: ClosedInterval<CGFloat> = CGFloat(指定したい範囲X軸の先端の座標)...CGFloat(指定したい範囲のX軸の末尾の座標)
let pictureYRange: ClosedInterval<CGFloat> = CGFloat(指定したい範囲Y軸の先端の座標)...CGFloat(指定したい範囲のY軸の末尾の座標)

// gestureRecognizer.viewタップされるView
let tapedCGPoint = gestureRecognizer.locationOfTouch(0, inView: gestureRecognizer.view)
let tapedPointXY = (tapedCGPoint.x, tapedCGPoint.y)

switch tapedPointXY {
case (pictureXRange, pictureYRange):  
    print("taped")
default:
    // 処理
}

問題

switch tapedPointXY {
case (pictureXRange, pictureYRange): 

をpictureXRange, pictureYRangeをまとめたいので

let pictureXYRange = (pictureXRange, pictureYRange)
switch tapedPointXY {
case pictureXYRange: 

と書きたいがなぜかエラーになってしまう。

3
4
4

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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?