Learn Courses My Dashboard

Delay not working in uiview.animate method

Hey Everyone,

I was playing around with some of the uivew.animate methods, and for some reason the delay is not working.

you can see that I have set the delay parameter for 5 seconds but the animation starts immediately

I know I am doing something wrong but can’t figure it out,

thanks for the help

@Abdul_Wahid

Post all of the code you have in your playground so that we can help you.

Paste your code in as text, rather than providing a screenshot.

Place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code so that it is formatted nicely. The back-tick character is located on the same keyboard key as the tilde character ~.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing rather than having to type the code in from an image.

import UIKit
import PlaygroundSupport

let liveViewFrame = CGRect(x: 0, y: 0, width: 500, height: 500)
let liveView = UIView(frame: liveViewFrame)

PlaygroundPage.current.liveView = liveView

liveView.backgroundColor = .black


let square = UIView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))

liveView.addSubview(square)
square.backgroundColor = .orange

UIView.animate(withDuration: 2.0, delay: 3.0, options: []) {
    square.backgroundColor = .purple
} completion: { (_) in
    
}

This is my code,
even tho the delay is set to 3.0 the animation starts immediately

I suspect that it is just the way Playground operates whether that is a feature or a fault I really don’t know.

If you do the same thing by creating a storyboard App the delay takes effect as expected.