2017-06-27 29 views
1

任何人都可以帮助我解决这个错误,请看下面的代码。代码从swift 2.0迁移到swift 3.0后出现错误

override func viewDidLoad() { 
    super.viewDidLoad() 
    ... 
    ... 

    //the following line occurs error: "Ambiguous use of 'startAnimation'" 
    **perform(#selector(UIViewAnimating.startAnimation), with: nil, afterDelay: 0.3)** 
} 

我该如何解决上述问题? enter image description here

+1

迁移到Swift 3.0之前的代码是什么? –

+2

为什么你甚至称这种方法?为什么不通过'perform()'而不是'UIViewAnimating.startAnimation(afterDelay:0.3)'? – Wukerplank

+0

请参阅[如何解决“模糊使用”编译错误与Swift#选择器语法?](https://stackoverflow.com/questions/35658334/how-do-i-resolve-ambiguous-use-of-compile- error-with-swift-selector-syntax)来解释问题。 –

回答

1

UIViewAnimating已经有一个延迟后启动动画的方法,你应该用这个来代替:

startAnimation(afterDelay: 0.3) 

如果你坚持用perform(_:with:afterDelay:)就可以这样使用它:

perform(NSSelectorFromString("startAnimation"), with: nil, with: 0.3) 

但请记住,您使用这种方法会降低编译器的安全性,并且如果您在选择器中输入拼写错误,您的应用程序将崩溃。

而且,你的代码是奇怪,因为UIViewController不符合UIViewAnimating,所以,除非你自己实现的(这是相当不寻常的),它是行不通的协议。更有可能在视图控制器中有一个名为startAnimation的方法,并且Swift migrator错误地添加了对UIViewAnimating的引用。在这种情况下,正确的代码将是:

perform(#selector(self.startAnimation), with: nil, afterDelay: 0.3) 
+0

非常感谢!你的建议有效!再次感谢! –

0

使用UIView类的方法。

+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations 



    UIView.animate(withDuration:duration){  
      // code to be executed  
    } 

看看这个UIView的方法文档https://developer.apple.com/documentation/uikit/uiview/1622515-animatewithduration和类似的查询Whats the Swift 3 animateWithDuration syntax?

+0

谢谢你的帮助,但是第一行似乎有点复杂,我会研究你以后回复〜 –

+0

@ K.Zeng请看那些链接。 – Rakesh