0

I am attempting to animate the alpha of a UIImage after 3 seconds has passed. By default the alpha is set to 0 and after 3 seconds, the alpha should change to 1, thus displaying the image to the user. My code I wrote for my animation does set the alpha to 0, but I am unable to change the the alpha to 1 after 3 seconds. I am newer to swift and not sure where I am going wrong with this. Here is my code.

import UIKit

class WelcomeOneViewController: UIViewController {

@IBOutlet weak var swipeImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    swipeImageView.alpha = 0
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    displaySwipeView()
}

func displaySwipeView() {
    UIView.animate(withDuration: 1.0, delay: 3.0, options: .beginFromCurrentState, animations: {

        DispatchQueue.main.async { [weak self] in
            self?.swipeImageView.alpha = 1
        }

    }, completion: nil)
}

}
4
  • If you want your animation should complete in 3 second then withDuration: 1.0, should be withDuration: 3.0, Commented Feb 13, 2018 at 13:45
  • I want the animation to begin after a 3 second delay, not take 3 seconds to complete. Commented Feb 13, 2018 at 13:45
  • From where are you calling this method ? Make sure it is view will appear not view did load and also try in main queue Commented Feb 13, 2018 at 13:46
  • I am calling the function in viewDidAppear with no luck Commented Feb 13, 2018 at 13:50

2 Answers 2

2

Try it with this code:

import UIKit

class WelcomeOneViewController: UIViewController {

    @IBOutlet weak var swipeImageView: UIImageView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        swipeImageView.alpha = 0
    }

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

    func displaySwipeView() {
        swipeImageView.alpha = 0
        UIView.animate(withDuration: 1.0, delay: 3.0, options: [], animations: {
            self.swipeImageView.alpha = 1
        }, completion: nil)
    }

}
Sign up to request clarification or add additional context in comments.

6 Comments

That didn't do the trick. The image is still visible immediately when the view loads.
@AndrewTuzsonhow about now?
That did the trick! However, now the image is visible for a split second before it disappears. How do I overcome that?
Would I use swipeImageView.alpha = 0 in viewWillAppear?
@AndrewTuzson that means that somewhere between viewDidLoad and viewDidAppear you are setting swipeImageView.alpha to 1 again.. try putting swipeImageView.alpha = 0 to viewWillAppear()
|
1

Try doing it on main queue like this:

func displaySwipeView() {
  UIView.animate(withDuration: 1.0, delay: 3.0, options: .beginFromCurrentState, animations: {

   DispatchQueue.main.async { [weak self] in
    self?.swipeImageView.alpha = 1
   }

  }, completion: nil)
}

Hope it helps!!

6 Comments

Thanks! I copied over your syntax and called displaySwipeView() in viewDidLoad() after setting the alpha to 0. The image is now visible when the screen loads. Any ideas?
So the problem now is, if I'm right, that image loads but without animation?
That is correct. The image is displayed immediately when the view is loaded. I have updated the syntax in the original post.
putting it on the main thread is useless.. viewDidLoad is executed on the main thread already
@AndrewTuzson Try using any other UIViewAnimationOptions. Other than .beginFromCurrentState try using .curveEaseInOut. beginFromCurrentState sometimes shows unexpected behaviour.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.