将UIImageView从子视图设置为父视图

时间:2016-04-22 00:10:50

标签: ios swift animation uiimageview

我正在开展一个允许用户下棋的项目。我有一个代表董事会的父视图,每个方块作为子视图添加到董事会。棋子是UIImageView,作为子视图添加到正方形。

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

要为国际象棋移动制作动画我想将棋子的位置从一个方格改为另一个方格,我明白这意味着改变UIImageView的框架,但我不知道如何,框架只是在它的父视图(正方形)中。这不起作用:

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)
}

1 个答案:

答案 0 :(得分:2)

考虑将您的棋子添加到棋盘本身而不是方块。这种架构允许您为任何移动设置动画,包括将一块放在两个方格之间的线上。

相关问题