Bird
0
0

What will happen when this Swift animation code runs? UIView.animate(withDuration: 1.0) { myView.layer.cornerRadius = 20 }

medium📝 Predict Output Q7 of 15
iOS Swift - Animations
What will happen when this Swift animation code runs? UIView.animate(withDuration: 1.0) { myView.layer.cornerRadius = 20 }
AThe corner radius will change instantly
BmyView will smoothly animate its corner radius to 20 over 1 second
Clayer properties cannot be changed
DThe code is missing a call to layoutIfNeeded()
Step-by-Step Solution
Solution:
  1. Step 1: Understand UIView.animate with layer properties

    UIView.animate can animate certain CALayer properties like cornerRadius when changed inside the closure.
  2. Step 2: Analyze the code effect

    The cornerRadius changes smoothly from its current value to 20 over 1 second.
  3. Final Answer:

    myView will smoothly animate its corner radius to 20 over 1 second -> Option B
  4. Quick Check:

    layer.cornerRadius animates with UIView.animate = B [OK]
Quick Trick: UIView.animate animates layer.cornerRadius [OK]
Common Mistakes:
  • Thinking cornerRadius isn't animatable via UIView.animate
  • Believing duration must be less than 1 second
  • Thinking layoutIfNeeded is required

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes