- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一堆动画添加到了 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/
我有一个这样的: CALayer *layer = stripeButton.layer; CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation a
我的类中有几个 CAKeyframeAnimation 对象。 他们都以 self 为代表。 在我的animationDidStop函数中,我如何知道调用来自哪里? 是否有任何变量可以传递给 CAKe
我为图层编写了动画,将图层从一个位置移动到另一个位置: - (void) animateCentreRightTransition: (int) transition { float scaleFac
我有一个菜单,它是一个 CALayer,它将在屏幕上滑动到给定点。我想要的效果是菜单会稍微超出该点,然后在该点之前一点,然后落在该点上。我可以通过应用变换来移动菜单,但我希望让这种弹跳效果发挥作用。我
现在有人如何使用 CAKeyframeAnimation 同时为多个图层设置动画吗?每个层都有自己的 CAKeyframeAnimation 对象。看一下下面的代码: 我有一个接收对象、创建 CAKe
我使用 View 的 CALayer 上的 CAKeyframeAnimation 来对 UIView 的框架属性进行动画处理,并对“位置”属性进行动画处理。我希望能够在用户点击屏幕时将动画停止在当前
这是我的代码: - (void)animateImages { self.emoImageView = [[UIImageView alloc] initWithFrame:CGRectMak
我有一堆动画添加到了 CALayer 中。我需要知道按钮在动画交互时的位置。但框架并没有改变。我如何找到/计算该值?这是代码: let positionY = CAKeyframeAnimat
我正在使用 CAKeyframeAnimation 来制作旋转动画。我正在使用按钮触发图层上的动画。 我想从上一个动画停止弧度开始下一个旋转动画。我用kCAFillModeForwards设置了fil
在制作关键帧动画时,我似乎忽略了明显的内容。我已经查看了许多代码示例,包括 CAKeyframeAnimation 文档中引用的 Apple 的 MoveMe,但是,我找不到会导致我所看到的情况的差异
我正在尝试创建一个动画,其中项目以交错的时间进入 View 。为此,我使用了 CAKeyframeAnimation,但为了实现惊人效果,我需要指定 beginTime。 问题是我还希望动画在完成时不
我需要为 UIImageView 添加从起点到终点以及之后从终点到起点的无限动画。我正在尝试这样: CAKeyframeAnimation *pathAnimation = [CAKeyframeAn
我想用 CAKeyframeAnimation 旋转一个 UIImageView。问题是阴影随物体旋转,产生非真实效果... The shadow must remain under the imag
我正在尝试使用以下代码制作一个简单的 CAKeyframeAnimation: - (void)viewDidLoad { [super viewDidLoad]; // Do any
谁能告诉我在动画运行时是否始终保证 CAKeyframeAnimation 中的关键帧以其准确值命中?或者……他们只是充当插值指南吗?例如如果我指定,比方说,路径上的 3 个点,让一些任意属性遵循 -
我正在尝试使用 CAKeyframeAnimation 为 UIImage 数组设置动画。理论上很简单。 帖子底部的示例代码。 我的问题是,在动画完成后,我有一个无法消除的巨大漏洞。 初始化 CAKe
在我的 iPhone 应用程序中,我有一个 UIButton,当另一个 UIButton 被按下时,我将它旋转 180 度进入 View ,然后当再次单击时,该按钮会进一步旋转 180 度回到它开始的
我有以下代码行来运行带有某些帧的动画。 - (void)setAnimation { /* creating a layer */ CALayer *layer = [[CALayer
目前我正在使用 CAKeyframeAnimation 沿路径移动 png 图像 30 秒。有什么方法可以在点击按钮时在 0-30 秒之间停止此移动图像? 最佳答案 这将删除动画...... [you
我的应用程序中有一个屏幕,用户可以在其中看到沿着弧线飞行的云。此移动是在 CAKeyframeAnimation 的帮助下实现的,并且可以正常工作。但我想改进动画,以便当用户打开屏幕时,云必须看起来已
我是一名优秀的程序员,十分优秀!