将图像居中(直接在中心)

Centering (directly in the centre) an image in a frame

我是 运行 LaunchScreen 显示图像的动画。

然后它会进入放置在主情节提要上方的视图,一旦动画完成,该视图就会显示下面的情节提要。

但是,重叠视图上的图像不会自行居中。我该怎么做?

我提供了下面的代码和图片来显示差异。 这是情节提要中 ViewController class 中的代码。


import UIKit

class ViewController: UIViewController {

    let arImage = UIImageView(image: UIImage(named: "CN")!)
    let splashView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        splashView.backgroundColor = UIColor(red: 255/255, green: 53/255, blue: 79/255, alpha: 1.0)
        // Colour of View
        view.addSubview(splashView)
        splashView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
        // View Bounds

        arImage.contentMode = .scaleAspectFit
        // Aspect Fill Of Image
        splashView.addSubview(arImage)
        arImage.frame = CGRect(x: splashView.frame.midX - 50, y: splashView.frame.midX - 50, width: 160, height: 160)
        // image sizing (not placement)

    }

    override func viewDidAppear(_ animated: Bool) {

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2){
            self.scaleDownAnimation()
        }
    }

    func scaleDownAnimation(){
        UIView.animate(withDuration: 0.5, animations: {
            self.arImage.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
            // shrinks

        }) { ( success) in

            self.scaleUpAnimation()

        }
    }


    func scaleUpAnimation() {

        UIView.animate(withDuration: 0.35, delay: 0.1, options: .curveEaseIn, animations: {
            self.arImage.transform = CGAffineTransform(scaleX: 5, y: 5)
        }) { ( success) in
            self.removeSplashScreen()
            // expands

        }
    }

    func removeSplashScreen() {
        splashView.removeFromSuperview()
    }
}

以下是显示启动屏幕和视图之间差异的图像:

LaunchScreen Just before the animation takes place

谢谢

您始终可以使用自动布局约束设置框架

    override func viewDidLoad() {
        super.viewDidLoad()

        splashView.backgroundColor = UIColor(red: 255/255, green: 53/255, blue: 79/255, alpha: 1.0)
        splashView.translatesAutoresizingMaskIntoConstraints = false


        view.addSubview(splashView)
        splashView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        splashView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        splashView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        splashView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true


        arImage.contentMode = .scaleAspectFit
        arImage.translatesAutoresizingMaskIntoConstraints = false

        splashView.addSubview(arImage)

        arImage.widthAnchor.constraint(equalToConstant: 160).isActive = true
        arImage.heightAnchor.constraint(equalToConstant: 160).isActive = true
        arImage.centerXAnchor.constraint(equalTo: splashView.centerXAnchor).isActive = true
        arImage.centerYAnchor.constraint(equalTo: splashView.centerYAnchor).isActive = true

    }