CALayer - setAnimationDuration不工作

时间:2018-03-07 18:11:03

标签: ios xcode calayer opacity layer

我只是想创建一个新的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()
                        }

                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

看起来像添加图层并试图在其中设置动态不透明度#34;一次去"不会工作。

你可以这样做:

    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"的图像添加到您的游乐场资源文件夹中):

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