phuong
phuong

Reputation: 423

Animate UIImageView from a subview to parent view

I'm working on a project allowing users play chess. I have a parent view which represents a board, each square is added to the board as a subview. Chess piece is an UIImageView and is added to a square as a subview.

occupyingPieceImageView = UIImageView(frame: CGRectMake(0, 0, size, size))
    self.addSubview(occupyingPieceImageView)

To animate a chess move I want to change the position of the piece from one square to an other, I understand that means changing the UIImageView's frame but I don't know how, the frame is only within it's parent view (square). This doesn't work:

func move(destSquare: Square){
    var imgView = self.occupyingPieceImageView
    var thisPiece = self.piece
    UIView.animateWithDuration(3, animations: {
        self.clearPiece()
        imgView.frame = destSquare.frame
    })
    destSquare.setPiece(thisPiece)
}

Upvotes: 2

Views: 318

Answers (1)

Terminus
Terminus

Reputation: 947

Consider adding your pieces to the board itself rather than to squares. This architecture would allow you to animate any movement, including even placing a piece on the line between two squares.

Upvotes: 2

Related Questions