Bird
0
0

Identify the error in this Swift animation code:

medium📝 Debug Q14 of 15
iOS Swift - Animations
Identify the error in this Swift animation code:
UIView.animate(withDuration: 0.5) {
  myView.alpha = 0
}
ANo error; code will animate alpha to 0 correctly.
BMissing completion handler closure.
CIncorrect method name; should be animateWithDuration.
DAnimation duration must be at least 1 second.
Step-by-Step Solution
Solution:
  1. Step 1: Check UIView.animate syntax

    The method UIView.animate(withDuration:) with a trailing closure is valid and animates changes inside the closure.
  2. Step 2: Verify animation correctness

    Setting alpha to 0 inside the closure animates the fade out correctly; no completion handler is required.
  3. Final Answer:

    No error; code will animate alpha to 0 correctly. -> Option A
  4. Quick Check:

    UIView.animate with closure is valid = B [OK]
Quick Trick: Trailing closure syntax is valid for UIView.animate [OK]
Common Mistakes:
  • Thinking completion handler is mandatory
  • Confusing method name with older Objective-C style
  • Believing duration must be longer than 0.5 seconds

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes