swift Swift - 渐变和背景与重新定位

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift - 渐变和背景与重新定位相关的知识,希望对你有一定的参考价值。

Source: SVG-Test

See: https://dzone.com/articles/calayer-and-auto-layout-with-swift-1

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imgBackground: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setBackgroundImage()
        view.layer.addSublayer(gradientLayer)
        gradientLayer.frame = view.bounds

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

//    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
//        super.viewWillTransition(to: size, with: coordinator)
//        print ("Orientation changed")
//        setBackgroundImage()
//        self.imgBackground.layer.frame = self.view.bounds
//    }
    

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        print ("Orientation changed")
        setBackgroundImage()
        gradientLayer.frame = view.bounds
    }
    
    func setBackgroundImage() {
        print ("setBackgroundImage")
        let orientation = UIDevice.current.orientation
        if orientation.isLandscape {
            imgBackground.image = UIImage(named: "orange-apple-2732x2732l.png")
            print ("Landscape")
        } else {
            imgBackground.image = UIImage(named: "orange-apple-2732x2732p.png")
            print("Portrait")
        }
    }

    let gradientLayer: CAGradientLayer = {
        let layer = CAGradientLayer()
        layer.colors = [
            UIColor.clear.cgColor,
            UIColor.blue.cgColor
        ]
        return layer
    }()
    
}

以上是关于swift Swift - 渐变和背景与重新定位的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 中的按钮上设置背景渐变

在 Swift 中制作渐变背景 - 正确的方法

swift 渐变背景

Swift : 动画 SKSpriteNode 渐变

Swift - UI 按钮阴影渐变

Swift UITableView中的渐变层