下記コードで、for文で15個のpiece(UIImageView)に対して、addGestureRecognizerでGestureを設定しているのですが、最後のpieceにだけGestureが設定されており、他には設定されまえせん。特にエラーも発生せず困っております。なぜでしょうか?
import UIKit
class ViewController: UIViewController {
var board: UIView!
var piece: UIImageView!
enum Direction {
case Left, Right, Up, Down, NG
}
let PieceSize: Int = 70
var empty: Int = 15
var panning : Bool = false
var pieceX: CGFloat = 0
var pieceY: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
board = UIView(frame: CGRectMake(0, 0, CGFloat(PieceSize * 4), CGFloat(PieceSize * 4)))
board.center = view.center
board.backgroundColor = UIColor.grayColor()
board.layer.borderColor = UIColor.blackColor().CGColor
board.layer.borderWidth = 1.0
view.addSubview(board)
let panGesture = UIPanGestureRecognizer(target: self, action: "pan:")
for i in 0...14 {
pieceX = CGFloat(i % 4 * PieceSize)
if i < 4 {
pieceY = CGFloat(0)
} else if i > 3 && i < 8 {
pieceY = CGFloat(PieceSize)
} else if i > 7 && i < 12 {
pieceY = CGFloat(PieceSize * 2)
} else if i > 11 {
pieceY = CGFloat(PieceSize * 3)
}
piece = UIImageView(frame: CGRectMake(pieceX, pieceY, CGFloat(PieceSize), CGFloat(PieceSize)))
piece.image = UIImage(named: i.description + ".png")
piece.userInteractionEnabled = true
piece.tag = i
piece.addGestureRecognizer(panGesture)
board.addSubview(piece)
}
for view in board.subviews {
print(view)
}
}
func movePiece(aPiece: UIImageView) {
if isMovable(aPiece.tag) != Direction.NG {
// ピースを移動する
var newFrame = CGRectMake(0, 0, CGFloat(PieceSize), CGFloat(PieceSize))
newFrame.origin.x = CGFloat(PieceSize * (empty % 4))
newFrame.origin.y = CGFloat(PieceSize * (empty / 4))
aPiece.frame = newFrame
// 空白位置の入れ替え
let oldEmpty = empty
empty = aPiece.tag
aPiece.tag = oldEmpty
}
}
func pan(gesture: UIPanGestureRecognizer) {
let aPiece = gesture.view as! UIImageView
let direction = isMovable(aPiece.tag)
if (direction != .NG) {
if (gesture.state == UIGestureRecognizerState.Changed) || (gesture.state == UIGestureRecognizerState.Ended) {
let translation = gesture.translationInView(aPiece)
// 空白位置の方向に応じて動かす方向を限定する
if (direction == .Left) && (translation.x < 0) {
// 左に動かす
panning = true
aPiece.center.x = aPiece.center.x + translation.x
} else if (direction == .Right) && (translation.x > 0) {
// 右方向に動かす
panning = true
aPiece.center.x = aPiece.center.x + translation.x
} else if (direction == .Up) && (translation.y < 0) {
// 上方向に動かす
panning = true
aPiece.center.y = aPiece.center.y + translation.y
} else if (direction == .Down) && (translation.y > 0) {
// 下方向に動かす
panning = true
aPiece.center.y = aPiece.center.y + translation.y
}
// 移動距離をリセット
gesture.setTranslation(CGPointZero, inView: aPiece)
}
}
// パンが完了したらピースを移動
if gesture.state == UIGestureRecognizerState.Ended {
if panning {
movePiece(aPiece)
}
panning = false
}
}
func isMovable(tapPos: Int) -> Direction {
var result: Direction = .NG
if empty == tapPos + 4 {
// 下に移動
result = .Down
} else if empty == tapPos - 4 {
// 上に移動
result = .Up
} else if empty == (tapPos - 1) && (tapPos % 4) != 0 {
// 左に移動
result = .Left
} else if empty == (tapPos + 1) && (tapPos % 4) != 3 {
// 右に移動
result = .Right
}
return result
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}