PetePete
PetePete

Reputation: 37

Animating UIIMageView Alpha on view did load

I am attempting to animate the background image of my HomeController.

I would like to fade the image in, as such I was hoping to animate it's alpha property.

I have found many answers on here around this, however many seem dated (obj c, iOS 3/4/5 etc) and following the recommended approach, I still cannot get this animation to work.

Can anyone please tell me what is wrong with my code?

I would like to animate backgroundImage. I have placed my animation code at the end of my setupView method.

It does not 'fade in' as I would like, essentially the alpha jumps straight to 0.7

import UIKit

class HomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupView()
    }

    private let backgroundImage: UIImageView = {
        let image = UIImageView()
        image.image = #imageLiteral(resourceName: "foggy").withRenderingMode(.alwaysOriginal)
        image.contentMode = .scaleToFill
       image.alpha = 0
        image.isOpaque = false

        return image
    }()

    private let currentLocation: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Southampton, UK", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 24)
        button.contentMode = .center
        button.setTitleColor(.black, for: .normal)
        button.addTarget(self, action: #selector(onPressLocation), for: .touchUpInside)

        return button
    }()

    private let currentTemp: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("37°C", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 54, weight: .thin)
        button.contentMode = .center
        button.setTitleColor(.white, for: .normal)
        button.addTarget(self, action: #selector(onPressTemp), for: .touchUpInside)

        return button
    }()

}

extension HomeController {

    @objc fileprivate func onPressLocation() -> Void {
        print("Location pressed.")
    }

    @objc fileprivate func onPressTemp() -> Void {
        self.currentTemp.setTitle("98°F", for: .normal)
    }

    fileprivate func setupView() -> Void {

        view.backgroundColor = .white

        // MARK: Background Image
        view.addSubview(backgroundImage)
        backgroundImage.anchor(
            top: view.safeAreaLayoutGuide.topAnchor,
            left: view.safeAreaLayoutGuide.leftAnchor,
            bottom: view.safeAreaLayoutGuide.bottomAnchor,
            right: view.safeAreaLayoutGuide.rightAnchor,
            paddingTop: 0,
            paddingLeft: 0,
            paddingBottom: 0,
            paddingRight: 0,
            width: 0,
            height: 0
        )

        // MARK: Current Location
        view.addSubview(currentLocation)
        currentLocation.anchor(
            top: view.safeAreaLayoutGuide.topAnchor,
            left: view.safeAreaLayoutGuide.leftAnchor,
            right: view.safeAreaLayoutGuide.rightAnchor,
            paddingTop: 50,
            paddingLeft: 0,
            paddingBottom: 0,
            paddingRight: 0,
            width: 0,
            height: 26,
            centerX: view.centerXAnchor
        )

        // MARK: Temp Button
        view.addSubview(currentTemp)
        currentTemp.anchor(
            top: currentLocation.bottomAnchor,
            paddingTop: 10,
            paddingLeft: 0,
            paddingBottom: 0,
            paddingRight: 0,
            width: 200,
            height: 70,
            centerX: view.centerXAnchor
        )

        // MARK: Animations
        UIView.animate(withDuration: 6, delay: 3, options: .curveEaseInOut, animations: {
            self.backgroundImage.alpha = 0.7
        }, completion: nil)

    }
}

Upvotes: 1

Views: 383

Answers (2)

mahendra solanki
mahendra solanki

Reputation: 31

You can put your code in viewDidAppear view life cycle method for any animation work ,because viewDidAppear method notifies the view controller that its view was added to a view hierarchy.Use below code for your solution.

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated: animated)
    //Call method like this 
      setupView()
   }

Upvotes: 1

iPeter
iPeter

Reputation: 1360

IBOutlets does not get visible when viewDidLoad is called so call your setupView method in viewDidAppear which is called after your view appeared on screen.

Hope this helps.

Upvotes: 2

Related Questions