2017-04-02 149 views
-5

如何将其转换为Swift 3?我试过,但我不断收到动画.fromValue和animation.toValue错误(这是只有2条线,我需要)转换为Swift 3

extension UIView { 

    func shake() { 
     let animation = CABasicAnimation(keyPath: "position") 
     animation.duration = 0.07 
     animation.repeatCount = 3 
     animation.autoreverses = true 

     // This line produces error 
     animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 10, self.center.y)) 
     // And this line produces error 
     animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 10, self.center.y)) 

     self.layer.addAnimation(animation, forKey: "position") 
    } 
} 
+3

什么是错误? –

回答

1

哇。它花了一些时间来让编译器不会抱怨,但这里是你的第一线,做正确:

animation.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - 10, y: self.center.y)) 

我说“哇”,因为相对于你的斯威夫特2行:

animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 10, self.center.y)) 

你基本上停留斯威夫特2和雨燕3之间事物之间:

  • CGPointMake现在简直是CGPoint。这很小,我想你会找到它的。
  • NSValue使用cgPoint的Swift 3语法,但是然后*定义该点意味着使用CGPoint

这种构建。但只有经过几次尝试,并让Xcode抱怨。我很惊讶升级到目前的语法没有抓住这个 - 但也许是因为有两个问题需要升级。

您可以将它用于第二次构建错误。如果那是完全错误的 - 语法 - 那么我相信你很好走。

+0

很多时候,升级并没有发现那些初始化改变。我注意到它几乎从来没有捕捉到像CGRectMake到CGRect和CGVectorMake到CGVector的东西 – TheValyreanGroup

+0

是的,但它是*组合*抛出我--NSValue需要“cgPoint”和CGRectMake需要“CGRect”。当然,一旦你看到它,没有什么大不了 - 但结合? (顺便说一下,我同意升级没有捕捉CGRectMake与CGRect,没有升级工具是完美的。) – dfd

+0

我曾尝试过,但我仍然得到这两行的箭头。 (我正在使用游乐场) –