dinis dinis - 1 month ago 9
Swift Question

Display image 5 second after hidden

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


After animation is completed the image is hidden but I would like to display again this after 5 second in original position. How i do this?

Answer

Well all you'd have to do is chain a new animation to the finish that does the opposite of the previous animation (move back to the original location and unhide) - Courtesy of @Rob's comment.

It'd just be:

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