gpt4 book ai didi

ios - CALayer 在 iPhone 旋转时自动旋转

转载 作者:行者123 更新时间:2023-11-28 17:48:31 27 4
gpt4 key购买 nike

我有一个 UIViewController,我在其中将 CALayer 子类添加到 View 层:

[self.view.layer addSublayer:myObject.backgroundLayer];

当我旋转设备时, View 会旋转,但 CALayer 不会。它有点被移到左边,仍然是纵向 View 。

有没有办法让子图层自动旋转,或者我是否需要应用变换?

最佳答案

在 Swift 4/iOS 11 中,您可以根据需要选择以下 6 个示例 之一来管理您的 CALayer/CAGradientLayer 设备旋转时的框架。

下面的示例使用 CAGradientLayer,但可以很容易地映射到 CALayerCAShapeLayer 的情况。


#1。覆盖 UIViewController viewDidLayoutSubviews()

import UIKit

class ViewController: UIViewController {

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

override func viewDidLoad() {
super.viewDidLoad()

view.layer.addSublayer(gradientLayer)
gradientLayer.frame = view.bounds
}

override func viewDidLayoutSubviews() {
gradientLayer.frame = view.bounds
}

}

#2。重写 UIViewController loadView(),继承 UIView 并重写 UIView layoutSubviews()

LayerView.swift

import UIKit

class LayerView: UIView {

lazy var gradientLayer: CAGradientLayer = {
let layer = CAGradientLayer()
layer.colors = [
UIColor.blue.cgColor,
UIColor.cyan.cgColor
]
self.layer.addSublayer(layer)
return layer
}()

override func layoutSubviews() {
gradientLayer.frame = bounds
}

}

LayerView.swift(备选)

import UIKit

class LayerView: UIView {

var gradientLayer: CAGradientLayer!

override func layoutSubviews() {
if gradientLayer == nil {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [
UIColor.blue.cgColor,
UIColor.cyan.cgColor
]
self.gradientLayer = gradientLayer
layer.addSublayer(gradientLayer)
}

gradientLayer.frame = bounds
}

}

ViewController.swift

import UIKit

class ViewController: UIViewController {

let layerView = LayerView()

override func loadView() {
view = layerView
}

}

#3。使用键值观察 (KVO)

import UIKit

class ViewController: UIViewController {

var observation: NSKeyValueObservation?

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

override func viewDidLoad() {
super.viewDidLoad()

view.layer.addSublayer(gradientLayer)

observation = view.observe(\.frame, options: [.new], changeHandler: { [unowned self] (object: UIView, change: NSKeyValueObservedChange<CGRect>) in
guard let frame = change.newValue else { return }
self.gradientLayer.frame = frame
})

// Also works
/*
observation = observe(\.view.frame, options: [.new], changeHandler: { [unowned self] (object: ViewController, change: NSKeyValueObservedChange<CGRect>) in
guard let frame = change.newValue else { return }
self.gradientLayer.frame = frame
})
*/
}

}

#4。重写 UIViewController loadView(),继承 UIView 并重写 UIView layerClass

LayerView.swift

import UIKit

class LayerView: UIView {

override public class var layerClass: AnyClass {
return CAGradientLayer.self
}

required init() {
super.init(frame: .zero)

guard let gradientLayer = layer as? CAGradientLayer else { return }
gradientLayer.colors = [
UIColor.blue.cgColor,
UIColor.cyan.cgColor
]
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

ViewController.swift

import UIKit

class ViewController: UIViewController {

let layerView = LayerView()

override func loadView() {
view = layerView
}

}

#5。覆盖 UIViewController loadView(),继承 UIView 并覆盖 CALayerDelegate layoutSublayers(of:)

LayerView.swift

import UIKit

class LayerView: UIView {

required init() {
super.init(frame: .zero)

let gradientLayer = CAGradientLayer()
gradientLayer.colors = [
UIColor.blue.cgColor,
UIColor.cyan.cgColor
]

layer.addSublayer(gradientLayer)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSublayers(of layer: CALayer) {
layer.sublayers?.forEach {
$0.frame = layer.bounds
}
}

}

ViewController.swift

import UIKit

class ViewController: UIViewController {

let layerView = LayerView()

override func loadView() {
view = layerView
}

}

#6。重写 UIViewController loadView(),子类 UIView,重写 UIView layerClass,子类 CALayer 和覆盖 CALayer layoutSublayers()

Layer.swift

import UIKit

class Layer: CALayer {

override init() {
super.init()

let gradientLayer = CAGradientLayer()
gradientLayer.colors = [
UIColor.blue.cgColor,
UIColor.cyan.cgColor
]

addSublayer(gradientLayer)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSublayers() {
sublayers?.forEach {
$0.frame = bounds
}
}

}

LayerView.swift

import UIKit

class LayerView: UIView {

override public class var layerClass: AnyClass {
return Layer.self
}

}

ViewController.swift

import UIKit

class ViewController: UIViewController {

let layerView = LayerView()

override func loadView() {
view = layerView
}

}

来源:

关于ios - CALayer 在 iPhone 旋转时自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1282302/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com