1

Can anyone help me resolving this error, just look at the following code.

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

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

How can I fix the above issue? enter image description here

Dharma
  • 3,007
  • 3
  • 23
  • 38
K. Zeng
  • 13
  • 2
  • 1
    What was the code before migrating to Swift 3.0 ? – Nirav D Jun 27 '17 at 08:43
  • 2
    Why do you even call the method like this? Why not just `UIViewAnimating.startAnimation(afterDelay: 0.3)` without going through `perform()`? – Wukerplank Jun 27 '17 at 08:51
  • See [How do I resolve “ambiguous use of” compile error with Swift #selector syntax?](https://stackoverflow.com/questions/35658334/how-do-i-resolve-ambiguous-use-of-compile-error-with-swift-selector-syntax) for an explanation of the problem. – Martin R Jun 27 '17 at 09:03
  • thank you guys! I really appreciate your help~ – K. Zeng Jun 27 '17 at 09:26

2 Answers2

1

UIViewAnimating already has a method to start the animation after a delay, you should use this one instead :

startAnimation(afterDelay: 0.3)

If you insist on using perform(_:with:afterDelay:) you can use it like so:

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

But keep in mind that you loose compiler safety with this approach, and if you make a typo in your selector your app will crash.

Also, your code is weird because UIViewController doesn't conform to UIViewAnimating, so unless you implemented the protocol yourself (which would be quite unusual), it's not going to work. It's more likely that you have a method called startAnimation in your view controller and that the Swift migrator mistakenly added the reference to UIViewAnimating. In which case the correct code would be:

perform(#selector(self.startAnimation), with: nil, afterDelay: 0.3)
deadbeef
  • 5,409
  • 2
  • 17
  • 47
0

Use UIView class method.

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



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

look at this UIView method documentation https://developer.apple.com/documentation/uikit/uiview/1622515-animatewithduration and similar query Whats the Swift 3 animateWithDuration syntax?

Rakesh
  • 29
  • 6