参数是未声明的类型?

时间:2017-01-16 17:54:21

标签: ios swift function parameters

我试图为某个功能设置参数,但控制台正在说明

  

使用未声明的类型func slideFromRight(from: rightInAnimation.fromValue = 25) { let rightInAnimation = CABasicAnimation(keyPath: "transform.translation.x") rightInAnimation.duration = 0.5 rightInAnimation.fromValue = 25 rightInAnimation.toValue = 0 rightInAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) self.layer.add(rightInAnimation, forKey: "animateTranslation") }

fromValue

我很困惑。我以为我不必在顶级声明这个。目的是在分配时能够更改{{1}}。

1 个答案:

答案 0 :(得分:1)

问题在于以下代码:

func slideFromRight(from: rightInAnimation.fromValue = 25)

您需要将其更改为:

func slideFromRight(from : Int = 25)
{
    let rightInAnimation            = CABasicAnimation(keyPath: "transform.translation.x")
    rightInAnimation.duration       = 0.5
    rightInAnimation.fromValue      = from
    rightInAnimation.toValue        = 0
    rightInAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

    self.layer.add(rightInAnimation, forKey: "animateTranslation")
}