我只是想创建一个新的 CALayer,用动画淡入淡出。但是,动画不起作用。该图层立即出现。
newLayer = CALayer()
newLayer.frame = self.view.frame
newLayer.opacity = 0
newLayer.contents = cgImage
newLayer.contentsGravity = kCAGravityResizeAspectFill
self.view.layer.insertSublayer(newLayer, above: self.oldLayer)
CATransaction.begin()
CATransaction.setAnimationDuration(1.5)
newLayer.opacity = 1
CATransaction.commit()
我在这里做错了什么?
更新
if let currentFilter = CIFilter(name: "CIGaussianBlur") {
currentFilter.setValue(clampFilter!.outputImage, forKey: "inputImage")
currentFilter.setValue(60.0, forKey: "inputRadius")
if let output = currentFilter.outputImage {
if let cgimg = self.context.createCGImage(output, from: inputImage.extent) {
if self.blurLayer == nil {
self.blurLayer = CALayer()
self.blurLayer!.frame = self.previewView.frame
self.blurLayer!.opacity = 0
self.blurLayer!.contents = cgimg
self.blurLayer!.contentsGravity = kCAGravityResizeAspectFill
self.view.layer.insertSublayer(self.blurLayer!, above: self.main.layer)
}
DispatchQueue.main.async {
CATransaction.begin()
CATransaction.setAnimationDuration(0.2)
self.previewBlurLayer!.opacity = 1
CATransaction.commit()
}
}
}
}
最佳答案
看起来添加图层并尝试“一次性”为其不透明度设置动画是行不通的。
你可以这样做:
self.view.layer.insertSublayer(newLayer, above: self.oldLayer)
DispatchQueue.main.async {
CATransaction.begin()
CATransaction.setAnimationDuration(1.5)
newLayer.opacity = 1
CATransaction.commit()
}
编辑:
这是一个可以在 Playground 页面中运行的完整示例(假设您已将名为“testimage.png”的图像添加到 playground 的 Resources 文件夹中):
import UIKit
import PlaygroundSupport
class TestViewController : UIViewController {
let btn: UIButton = {
let b = UIButton()
b.translatesAutoresizingMaskIntoConstraints = false
b.setTitle("Tap Me", for: .normal)
b.backgroundColor = .red
return b
}()
let theView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .blue
return v
}()
let oldLayer: CALayer = {
let c = CALayer()
return c
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
view.addSubview(btn)
btn.addTarget(self, action: #selector(didTap(_:)), for: .touchUpInside)
btn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
btn.topAnchor.constraint(equalTo: view.topAnchor, constant: 20.0).isActive = true
view.addSubview(theView)
theView.widthAnchor.constraint(equalToConstant: 250.0).isActive = true
theView.heightAnchor.constraint(equalToConstant: 250.0).isActive = true
theView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
theView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
oldLayer.frame = theView.bounds
oldLayer.backgroundColor = UIColor.red.cgColor
theView.layer.addSublayer(oldLayer)
}
@objc func didTap(_ sender: Any?) -> Void {
print("Tapped!")
let img = UIImage(named: "testimage")
let newLayer = CALayer()
newLayer.frame = theView.bounds
newLayer.opacity = 0
newLayer.contents = img?.cgImage
newLayer.contentsGravity = kCAGravityResizeAspectFill
theView.layer.insertSublayer(newLayer, above: oldLayer)
DispatchQueue.main.async {
CATransaction.begin()
CATransaction.setAnimationDuration(1.5)
newLayer.opacity = 1
CATransaction.commit()
}
}
}
let vc = TestViewController()
PlaygroundPage.current.liveView = vc
关于ios - CALayer - setAnimationDuration 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49158635/