【问题标题】:Animate UIImageView from a subview to parent view将 UIImageView 从子视图动画化到父视图
【发布时间】:2016-04-22 08:16:12
【问题描述】:

我正在开发一个允许用户下棋的项目。我有一个代表板的父视图,每个方块都作为子视图添加到板上。棋子是一个 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)
}

【问题讨论】:

    标签: ios swift animation uiimageview


    【解决方案1】:

    考虑将您的棋子添加到棋盘本身而不是正方形。这种架构可以让您为任何运动设置动画,甚至包括在两个正方形之间的线上放置一块。

    【讨论】:

    • 我也在考虑这样做,但为此我需要更改设计。还有其他选择吗?
    • 您可以做的是将所有方块上的 clipsToBounds 设置为 false。然后,您可以使用原点在视图之外的帧(例如 (-100, -100, 50, 50) )为移动设置动画。动画结束后,在完成块中,您可以重新计算新正方形的坐标并执行 removeFromSuperView 并立即在新正方形上添加Subview。这确实效率低下,但应该可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多