隐藏后显示图像5秒

时间:2016-10-12 20:28:54

标签: swift uiimage uiviewanimation

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // Set the image view in the starting location
    moveobj.frame = CGRect(x: 100, y: 100, width: /* some width */,                   
    height: /* some height */)

    UIView.animate(withDuration: /* some duration */, animations: {
        // Move image view to new location
        self.moveobj.frame = CGRect(x: 300, y: 300, width: self.moveobj.frame.width, height: self.moveobj.frame.height)
    }) { (finished) in
        // Animation completed; hide the image view
        self.moveobj.isHidden = true
    }
}

动画完成后图像被隐藏但我希望在原始位置5秒后再次显示。我怎么这样?

1 个答案:

答案 0 :(得分:2)

所有你需要做的就是将一个新的动画链接到与前一个动画相反的结束(回到原始位置并取消隐藏) - 感谢@ Rob的评论。

它只是:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // Set the image view in the starting location

    let originalFrame = CGRect(x: 100, y: 100, width: /* some width */,                   
    height: /* some height */)
    moveobj.frame = originalFrame

    UIView.animate(withDuration: /* some duration */, animations: {
        // Move image view to new location
        self.moveobj.frame = CGRect(x: 300, y: 300, width: self.moveobj.frame.width, height: self.moveobj.frame.height)
    }) { (finished) in
        // Animation completed; hide the image view
        self.moveobj.isHidden = true
        // Next animation
        DispatchQueue.main.asyncAfter(deadline: .now() + 5){
            self.moveobj.frame = originalFrame
            self.moveobj.isHidden = false
        }
    }
}