ios - 动画 CALayer

标签 ios swift uiview calayer

我在 UIView 之上有一个 CALayer。

UIView 完全按照我想要的方式进行动画处理,但是它上面的 PlayerView 似乎“生成”屏幕外,然后动画化到 UIView 在然后开始的位置 它将动画化到 UIViews 框架。

我想知道 CALayers 是否有任何属性可以自动处理这个问题,但我没有找到任何东西。

代码如下:

    let window = UIApplication.shared.keyWindow!
     v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
     let v2 = UIView(frame: .zero)
    window.addSubview(v!);
    window.addSubview(v2)
    v?.backgroundColor = UIColor.black
    v2.backgroundColor = UIColor.clear

    v?.layer.cornerRadius = (v?.frame.width)! * 0.025






    guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
        debugPrint("video.m4v not found")
        return
    }

    player = AVPlayer(url: URL(fileURLWithPath: path))

    playerLayer = AVPlayerLayer(player: player)
    //        playerLayer.frame = playerView.frame

    playerLayer?.frame = (playerView?.bounds)!
    playerLayer?.masksToBounds = true

    playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill


    v?.layer.addSublayer(playerLayer!)

    player?.play()
    player?.isMuted = true

    //looper
    NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: nil)
    { notification in
        let t1 = CMTimeMake(5, 100)
        self.player?.seek(to: t1)

        self.player?.play()
    }







    let superview = playerView?.superview

    v2.frame = (v2.convert((playerView?.frame)!, from: superview))
            v?.frame = (v?.convert((playerView?.frame)!, from: superview))!

    window.addConstraintsWithFormat("H:|[v0]|", views: v!)
    window.addConstraintsWithFormat("V:|[v0]|", views: v!)

            playerLayer?.frame = (v?.frame)!



                self.playerLayer?.frame = (self.v?.frame)!


    UIView.animate(withDuration: 4, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {



        self.window?.layoutIfNeeded()

    }, completion: { (completed) in


        self.playerLayer?.frame = (self.v?.frame)!



    })


}

有什么建议吗?

编辑:

我已经包含了更新的代码

    let window = UIApplication.shared.keyWindow!
    v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
    let v2 = UIView(frame: .zero)
    window.addSubview(v!);

    v?.addSubview(playerView2!)

    window.addSubview(v2)
    v?.backgroundColor = UIColor.black
    v2.backgroundColor = UIColor.clear

    v?.layer.cornerRadius = (v?.frame.width)! * 0.025










    playerView2?.layer.cornerRadius = (playerView2?.bounds.width)! * 0.025

    guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
        debugPrint("video.m4v not found")
        return
    }

    player = AVPlayer(url: URL(fileURLWithPath: path))

    playerLayer = AVPlayerLayer(player: player)
    //        playerLayer.frame = playerView.frame

    playerLayer?.frame = (playerView2?.bounds)!
    playerLayer?.masksToBounds = true
    playerLayer?.cornerRadius = (playerView2?.bounds.width)! * 0.025

    playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill


    self.playerView2?.layer.addSublayer(playerLayer!)

//        player?.play()
    player?.isMuted = true

    //looper
    NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: nil)
    { notification in
        let t1 = CMTimeMake(5, 100)
        self.player?.seek(to: t1)

//            self.player?.play()
    }










    self.playerView2?.frame = (self.v?.bounds)!
    self.playerLayer?.frame = (self.playerView?.bounds)!





    UIView.animate(withDuration: 5, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {



        self.v?.frame = window.frame

        self.playerView2?.frame = (self.v?.frame)!

        self.playerLayer?.frame = (self.playerView2?.frame)!



        self.window?.layoutIfNeeded()

    }, completion: { (completed) in




    })





}

最佳答案

当您设置 playerLayer.frame 时,您将获得一个隐式动画。

如果您不需要任何动画,可以像这样暂时禁用隐式动画:

        UIView.animate(withDuration: 4, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.window?.layoutIfNeeded()
        }, completion: { (completed) in
            let priorValue = CATransaction.disableActions()
            CATransaction.setDisableActions(true)
            self.playerLayer?.frame = self.v!.frame
            CATransaction.setDisableActions(priorValue)
        })

如果你确实想为 playerLayer.frame 设置动画,那么最好的方法是创建一个 UIView 子类,它使用 AVPlayerLayer 作为它的层,像这样:

class PlayerView: UIView {
    override class var layerClass: AnyClass { return AVPlayerLayer.self }
    private(set) lazy var playerLayer: AVPlayerLayer = { self.layer as! AVPlayerLayer }()
}

然后您可以使用 PlayerView 而不是裸 AVPlayerLayer 并使用 UIKit 动画。由于您无法直接初始化 AVPlayerLayer(UIView 使用默认初始化程序来创建其层),因此您需要设置 PlayerView的播放器是这样的:

        playerView?.playerLayer = player

关于ios - 动画 CALayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44621709/

相关文章:

ios - 当首先加载 gameScene 时可以工作,但是当被另一个场景加载时播放半秒并立即停止(没有错误),为什么?

swift - macOS:从 CharacterPalette 获取表情符号(修订版)

swift - 如何从 Swift textView 获取文本?

ios - Swift NSAttributedString 自定义字体

iphone - 在 iOS 上,自定义 View 不会设置动画?

ios - 如何仅提供回调函数或 block 而不是创建符合 UIPickerViewDelegate 的对象?

ios - 当应用程序进入前台时,如何重新启动基于 block 的动画?

ios - 如何从 crashlytics 禁用崩溃报告弹出窗口

ios - UIScrollView的 "pull up to load more"和 “pull down to go back"导致闪烁(附示例代码)

ios - 在 iOS Swift 中设置按钮事件颜色的简单方法