gpt4 book ai didi

ios - CAKeyframeAnimation 组位置

转载 作者:行者123 更新时间:2023-11-30 11:52:58 26 4
gpt4 key购买 nike

我有一堆动画添加到了 CALayer 中。我需要知道按钮在动画交互时的位置。但框架并没有改变。我如何找到/计算该值?这是代码:

    let positionY = CAKeyframeAnimation(keyPath: "position.y")
positionY.values = valuesY
positionY.keyTimes = keyTimes
positionY.duration = totalTime
positionY.isAdditive = true
positionY.isRemovedOnCompletion = false
// X Position Animation
let positionX = CAKeyframeAnimation(keyPath: "position.x")
positionX.values = valuesX
positionX.keyTimes = keyTimes
positionX.duration = 5
positionX.isAdditive = true
positionX.isRemovedOnCompletion = false
// Rotation Animation
let rotation = CAKeyframeAnimation(keyPath: "transform.rotation.z")
rotation.values = valuesRotation
rotation.keyTimes = keyTimes
rotation.duration = totalTime
rotation.isAdditive = true
rotation.isRemovedOnCompletion = false
// Grouping the animations
let group = CAAnimationGroup()
group.animations = [positionX, positionY, rotation]
group.duration = totalTime
group.fillMode = kCAFillModeForwards
group.delegate = self
group.setValue(button, forKey: GiftGameVC.kAnimationKeyPath)
group.isRemovedOnCompletion = false
//
CATransaction.begin()
CATransaction.setDisableActions(false)
CATransaction.setCompletionBlock {
print("Done")
}
button.layer.add(group, forKey: GiftGameVC.kAnimationKeyPath)

CATransaction.commit()

最佳答案

您将需要检查按钮的presentationLayer,因为您基本上是在观看电影,并且真实的层位置永远不会像您发现的那样改变。一个例子如下所示。此外,对于图层动画, View 的框架永远不会改变,只会改变图层位置和图层框架。 UIView动画可以更新 View 的框架。

import UIKit

class ViewController: UIViewController {

var timer : Timer?
var animationView = UIView()
var currentPositionLabel = UILabel()

override func viewDidLoad() {
super.viewDidLoad()
animationView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
animationView.layer.cornerRadius = 15
animationView.backgroundColor = UIColor.blue
self.view.addSubview(animationView)

let button = UIButton(frame: CGRect(x: 20, y:self.view.bounds.height - 60, width: self.view.bounds.width - 40, height: 40))
button.addTarget(self, action: #selector(startAnimation), for: .touchUpInside)
button.backgroundColor = UIColor.green
button.setTitle("Restart Animation", for: .normal)
self.view.addSubview(button)

currentPositionLabel = UILabel(frame: CGRect(x: 20, y:button.frame.minY - 50, width: self.view.bounds.width - 40, height: 40))
currentPositionLabel.textColor = UIColor.gray
currentPositionLabel.adjustsFontSizeToFitWidth = true
currentPositionLabel.minimumScaleFactor = 0.01
self.view.addSubview(currentPositionLabel)

currentPositionLabel.text = "Current Position \(animationView.layer.position)"
}

@objc func startAnimation(){

//prepare to watch a movie/animation while your layer never really moves
CATransaction.begin()
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 20.0
animation.toValue = NSValue(cgPoint: CGPoint(x: self.view.bounds.width - 35, y: self.view.bounds.height - 120))
CATransaction.setCompletionBlock {
self.timer?.invalidate()
self.timer = nil
print("the real position never changed \(self.animationView.layer.position)")
self.animationView.layer.removeAllAnimations()
self.currentPositionLabel.text = "Current Position \(self.animationView.layer.position)"
}
animationView.layer.add(animation, forKey: "animationViewPosition")
//if you really want to update the real position of the layer i do it here
//self.animationView.layer.position = CGPoint(x: self.view.bounds.width - 35, y: self.view.bounds.height - 120)
//this would mean when the animation is removed that the layer is there but still would not give you the current value
//of the layer position
CATransaction.commit()

if let _ = timer{
//do nothing we are good to go
}else{
//start a new timer
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(timerUpdateAnimationPosition), userInfo: nil, repeats: true)
}
}

@objc func timerUpdateAnimationPosition(){
//get presentation layer
if let presLayer = self.animationView.layer.presentation(){
if let animationPosition = presLayer.value(forKeyPath: "position") as? CGPoint{
self.currentPositionLabel.text = "Animation Position : \(animationPosition)"
}

}
}
}

您可以在函数timerUpdateAnimationPosition中看到我检查了presentationLayer的动画位置。有关动画位置处的交互,请参阅此 answer 。但如果这就是您正在寻找的内容,则意味着上面的答案也是正确的。

关于ios - CAKeyframeAnimation 组位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48145652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com