//
|
// 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?
|
private var aPNGActionImageView: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")!){
|
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(){
|
|
if let waterApngImage = try? APNGImage(fileURL: Bundle.main.url(forResource: "apngb-animated-flow", withExtension:"png")!){
|
waterApngImage.numberOfPlays = 2
|
aPNGActionImageView = APNGImageView(image: waterApngImage)
|
aPNGActionImageView!.alpha = 0
|
aPNGSunImageView?.contentMode = .scaleToFill
|
view.addSubview(aPNGActionImageView!)
|
aPNGActionImageView!.snp.makeConstraints { make in
|
make.center.equalToSuperview().inset(UIEdgeInsets(top: 90, left: 140, bottom: 0, right: 0))
|
make.width.equalTo(200)
|
make.height.equalTo(150)
|
}
|
UIView.animate(withDuration: 0.5) {
|
self.aPNGActionImageView!.alpha = 1.0
|
}
|
aPNGActionImageView!.startAnimating()
|
|
aPNGActionImageView!.onOnePlayDone.delegate(on: self) {[unowned self] a,b in
|
if b == 2{
|
UIView.animate(withDuration: 0.5) {
|
self.aPNGActionImageView!.alpha = 0
|
}completion: { _ in
|
self.aPNGActionImageView!.removeFromSuperview()
|
}
|
}
|
}
|
}
|
|
|
|
// 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"{
|
|
|
}
|
}
|
}
|