Xcode CABasic动画转换不工作
我正在使用Xcode开发一个应用程序,并试图让一个圆圈改变它的x位置。我已经写了下面的代码没有用,代码没有给我一个错误,它根本不工作。任何帮助都将不胜感激。 我的代码如下Xcode CABasic动画转换不工作,xcode,swift,Xcode,Swift,我正在使用Xcode开发一个应用程序,并试图让一个圆圈改变它的x位置。我已经写了下面的代码没有用,代码没有给我一个错误,它根本不工作。任何帮助都将不胜感激。 我的代码如下 func changeNotes() { switch notesState { case 1: let animation = CABasicAnimation() animation.keyPath = "transform.t
func changeNotes() {
switch notesState {
case 1:
let animation = CABasicAnimation()
animation.keyPath = "transform.translate.x"
animation.fromValue = 668
animation.toValue = 55
animation.duration = 0.2
notesButton.layer.addAnimation(animation, forKey: "transformx")
notesState = 0
default:
let animation = CABasicAnimation()
animation.keyPath = "transform.translate.x"
animation.fromValue = 55
animation.toValue = 668
animation.duration = 0.2
notesButton.layer.addAnimation(animation, forKey: "transformx")
notesState++
有什么帮助吗
let translateX = CABasicAnimation(keyPath: "position") // declare it out of your function inside your view controller class just below any IBOulets you may have
translateX.fromValue = NSValue(CGPoint: CGPointMake(668,notesButton.layer.position.y))
translateX.toValue = NSValue(CGPoint: CGPointMake(55,notesButton.layer.position.y))
translateX.duration = 0.2
notesButton.layer.addAnimation(translateX, forKey: "position")
编辑
不需要在那里切换用例。您可以这样尝试:
let translateX = CABasicAnimation(keyPath: "position") // declare it out of your function inside your view controller class just below any IBOulets you may have
if notesButton.layer.position.x == 668 {
translateX.fromValue = NSValue(CGPoint: CGPointMake(668,notesButton.layer.position.y))
translateX.toValue = NSValue(CGPoint: CGPointMake(55,notesButton.layer.position.y))
notesButton.layer.position.x = 55
translateX.duration = 0.2
} else {
translateX.fromValue = NSValue(CGPoint: CGPointMake(55,notesButton.layer.position.y))
translateX.toValue = NSValue(CGPoint: CGPointMake(668,notesButton.layer.position.y))
notesButton.layer.position.x = 55
translateX.duration = 0.2
}
notesButton.layer.addAnimation(translateX, forKey: "position")
它仍然不起作用,我做得对吗?我很抱歉,如果我遗漏了一些明显的东西,我对这件事还是相当陌生的。(因为代码不合适,所以将其作为图像上载)可能值得一提的是,它位于由相同函数更改触发的另一个动画下:顶部的动画有效。@Bobjonese您必须为函数选择不同的名称。将其更改为func changeRadius(){…},另一个更改为fund changePosition(){…},如下所示:?因为它不工作,按钮(是一个圆圈)只是在屏幕上飞过并卡住了。你在哪里声明了你的方法?你能给我看看吗。我告诉过你在你的行动之外申报你的VAR