dinis02
dinis02

Reputation: 17

Display and move image

I have this piece of code that makes an image move, but I want this to be displayed 5 seconds after I open a page.

How do I do this? Thanks

@IBOutlet weak var moveobj: UIImageView!



override func viewDidAppear(animated: Bool) {


    super.viewDidAppear(animated)


    let orbit = CAKeyframeAnimation(keyPath: "position")
    var affineTransform = CGAffineTransformMakeRotation(0.0)
    affineTransform = CGAffineTransformRotate(affineTransform, CGFloat(M_PI))
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: 198 - (100/2),y: 135 - (100/2)), radius:  CGFloat(155), startAngle: CGFloat(255), endAngle:CGFloat(M_PI * 2 ), clockwise: true)


    orbit.path = circlePath.CGPath
    orbit.duration = 18
    orbit.additive = true
    orbit.repeatCount = 0.15
    orbit.calculationMode = kCAAnimationPaced


    moveobj.layer .addAnimation(orbit, forKey: "orbit")

Upvotes: 0

Views: 69

Answers (2)

Idan
Idan

Reputation: 5450

I'm not sure if it what you are looking for. Aside from using a timer like Rashwan suggested, you can use GCD.

In viewDidAppear(), show the picture, then add the following code for 5 seconds delay, Something like this:

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

    loadImage()

    let delayInSeconds = 5.0
    let popTime = dispatch_time(DISPATCH_TIME_NOW,
        Int64(delayInSeconds * Double(NSEC_PER_SEC)))
    dispatch_after(popTime, dispatch_get_main_queue()) {
    //You can add some "finish up" operations here 
    }
}

Upvotes: 1

Rashwan L
Rashwan L

Reputation: 38833

To wait 5 seconds before you do anything you can use a Timer, see below how to use it:

override func viewDidLoad() {
    super.viewDidLoad()

    // start a new timer with an interval of 5 seconds
    Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(ViewController.showImage), userInfo: nil, repeats: false)
}

// after 5 seconds this class will be called and here you can do your work
func showImage(){
    print("Showing")
}

Upvotes: 0

Related Questions