Ios 如何从停止点恢复动画?
我有Ios 如何从停止点恢复动画?,ios,swift,animation,core-animation,Ios,Swift,Animation,Core Animation,我有ViewController()和Circle.swift 在我的Circle.swift中,我有所有的动画代码,从ViewController我只调用它 因此,在Circle文件中,我有: func commonSetup() { ... fillingLayer.strokeStart = start anim.fromValue = start anim.toValue = end fillingLayer.strokeEnd = end
ViewController()
和Circle.swift
在我的Circle.swift
中,我有所有的动画代码,从ViewController
我只调用它
因此,在Circle文件中,我有:
func commonSetup() {
...
fillingLayer.strokeStart = start
anim.fromValue = start
anim.toValue = end
fillingLayer.strokeEnd = end
fillingLayer.addAnimation(anim, forKey: "circleAnim")
}
其中,我在类圆上方设置start=0.0
和end=0.55
:UIView{
在我的ViewController
中,我有一个按钮,点击它我调用:
func pressed() {
start = 0.55
end = 0.9
Circle().commonSetup()
}
但它不工作。我的动画无法恢复。我如何修复它?我想首先从0加载到0.55,然后单击按钮-从0.55加载到0.9
我在代码中看不到的内容?您不应该经常更改
的值,即从value
、更改为value
和strokeStart
:
func commonSetup() {
...
fillingLayer.strokeStart = 0;
anim.fromValue = START_VALUE //value expressed in points
anim.toValue = END_VALUE //value expressed in points
fillingLayer.strokeEnd = end
fillingLayer.addAnimation(anim, forKey: "circleAnim")
}
您应该设置
fromValue
、toValue
和strokeStart
属性的值,并且不要更改它们。您只能通过更改strokeEnd
属性来控制动画。问题是,当您说circle().commonSetup()时,您正在创建一个新的circle实例
您需要在之前创建的circle实例上调用commonSetup
所以看起来可能更像这样。在视图控制器中,有一个圆的变量
let progressCircle = Circle()
然后,在按下的功能中:
func pressed() {
start = 0.55
end = 0.9
progressCircle.commonSetup()
}
如果希望将其用作类函数而不是实例函数(对于类似的情况,这可能是不正确的),则需要使用class
关键字定义commonSetup
函数,如下所示:
class func commonSetup() {
//code
}
然后,您可以像这样使用它:Circle.commonSetup()
。请注意圆圈上缺少括号。
。这是因为您正在运行一个类函数,而不是初始化该类的新实例。但是……在您的情况下,将其作为类函数会很奇怪,因为您的函数实际上是在处理实例,而不是类级别的操作
我通常也习惯于在动画中使用适当的关键点。因此,你会说:
fillingLayer.addAnimation(anim,forKey:“strokeEnd”)
为笔划设置动画时,起始值和结束值是0.0-1.0,而不是点。