杨锴
2024-08-28 611f271e03e9ff2b5c32a9bbb2e3eb719c178df5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
//  TreeTeskVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/23.
//
 
import UIKit
import AVKit
import JQTools
import APNGKit
 
class TreeTeskVC: BaseVC {
 
                private lazy var player:AVPlayer = {
                                let bgPath = Bundle.main.url(forResource: "bg", withExtension: "mov")
                                let p = AVPlayer(url: bgPath!)
                                p.isMuted = true
                                return p
                }()
 
                private lazy var playerLayer:AVPlayerLayer = {
                                 let pLayer = AVPlayerLayer()
                                pLayer.videoGravity = .resize
                                return pLayer
                }()
 
                private var treeImage = UIImageView()
                private var aPNGTreeImageView:APNGImageView?
                private var aPNGSunImageView:APNGImageView?
 
 
                override func viewDidDisappear(_ animated: Bool) {
                                super.viewDidDisappear(animated)
                                player.pause()
                                aPNGSunImageView?.stopAnimating()
                                aPNGTreeImageView?.stopAnimating()
                }
 
                override func viewDidAppear(_ animated: Bool) {
                                player.play()
                                aPNGSunImageView?.startAnimating()
                                aPNGTreeImageView?.startAnimating()
                }
 
                override func viewDidLayoutSubviews() {
                                super.viewDidLayoutSubviews()
                                playerLayer.frame = view.frame
                }
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
 
    }
 
                override func setUI() {
                                playerLayer.player = player
                                view.layer.addSublayer(playerLayer)
                                player.play()
 
 
                                if let sunApngImage = try? APNGImage(fileURL: Bundle.main.url(forResource: "apngb-animated_sun", withExtension: "png")!){
                                                sunApngImage.numberOfPlays = 2
                                                aPNGSunImageView = APNGImageView(image: sunApngImage)
                                                view.addSubview(aPNGSunImageView!)
                                                aPNGSunImageView!.snp.makeConstraints { make in
                                                                make.edges.equalToSuperview()
                                                }
                                                aPNGSunImageView!.startAnimating()
                                }
 
 
                                if let treeApngImage = try? APNGImage(fileURL: Bundle.main.url(forResource: "apngb-animated", withExtension: "png")!,decodingOptions: .notCacheDecodedImages){
                                                treeApngImage.numberOfPlays = 1
                                                aPNGTreeImageView = APNGImageView(image: treeApngImage)
                                                let tap = UITapGestureRecognizer(target: self, action: #selector(jumpAction))
                                                aPNGTreeImageView!.addGestureRecognizer(tap)
                                                view.addSubview(aPNGTreeImageView!)
                                                aPNGTreeImageView!.snp.makeConstraints { make in
                                                                make.edges.equalToSuperview()
                                                }
                                                aPNGTreeImageView!.startAnimating()
                                }
                }
 
                override func setRx() {
                                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { [weak self] _ in
                                                self?.player.seek(to: CMTime.zero)
                                                self?.player.play()
                                }
 
                                NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { [weak self] _ in
                                                self?.player.pause()
                                                self?.aPNGSunImageView?.stopAnimating()
                                                self?.aPNGTreeImageView?.stopAnimating()
                                }
 
                                NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { [weak self] _ in
                                                if self?.navigationController?.tabBarController?.selectedIndex == 2{
                                                                self?.player.play()
                                                                self?.aPNGSunImageView?.startAnimating()
                                                                self?.aPNGTreeImageView?.startAnimating()
                                                }
                                }
                }
 
                @objc func jumpAction(){
                                let springAnimation = CASpringAnimation(keyPath: "transform.scale.y")
                                springAnimation.fromValue = 1
                                springAnimation.toValue = 1.06
                                springAnimation.initialVelocity = 0.5 //初速度
                                springAnimation.repeatCount = 1
                                springAnimation.duration = 3.0
                                springAnimation.mass = 0.4 //增加该值会增大弹性效果,即震动次数更多、幅度更大
                                springAnimation.stiffness = 100 //增大stiffness会减少震动次数,减小
                                springAnimation.damping = 1
                                springAnimation.isRemovedOnCompletion = false
                                springAnimation.fillMode = .forwards
                                aPNGTreeImageView?.layer.add(springAnimation, forKey: nil)
                }
}
 
extension TreeTeskVC:CAAnimationDelegate{
                func animationDidStart(_ anim: CAAnimation) {
                                print("开始动画")
                                treeImage.isUserInteractionEnabled = false
                }
 
                func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
                                print("结束动画")
                                if anim.value(forKey: "customType") as! String == "ani_flower"{
 
 
                                }
                }
}