使 UIImageView 下降

make UIImageView fall

UIImageview 有自己的位置,点击它之后(用 tapgesture 完成)我希望那个视图落到地面,直到位置为 0。我试着用 while 来实现循环但似乎不起作用。有什么解决办法吗?

    var bananaView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

    @objc func handleTap(_ sender:UITapGestureRecognizer) {
        let centerPosition = bananaView.frame.origin.y
        while centerPosition >= 0 {
            bananaView.layer.position = CGPoint(x: 0,y : centerPosition - 1)
        }
    }

您为其设置了新的位置(0)。然后将布局更新包装在动画块中

@objc func handleTap(_ sender:UITapGestureRecognizer) {
    let centerPosition = bananaView.frame.origin.y
    Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
        if bananaView.frame.origin.y - bananaView.frame.height == 0 {
            timer.invalidate()
        } else {
            bananaView.frame.origin.y -= 1
        }
    }
}