0
0
iOS Swiftmobile~20 mins

Spring animations in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Animation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the visible effect of this spring animation code?
Consider this Swift code snippet for a UIView animation using spring damping. What will the user see when this code runs?
iOS Swift
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.5, options: [], animations: {
  view.frame.origin.y += 100
}, completion: nil)
AThe view moves down with a slight bounce and settles quickly.
BThe view moves down quickly and bounces several times before settling.
CThe view moves down instantly without animation.
DThe view moves down smoothly and slowly without bouncing.
Attempts:
2 left
💡 Hint
Lower damping values cause more bounce in spring animations.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses spring animation with completion handler?
Select the Swift code snippet that correctly performs a spring animation on a view's alpha property and runs a completion block after animation.
A
UIView.animate(duration: 0.5, delay: 0, damping: 0.7, velocity: 1, options: [], animations: {
  view.alpha = 0
}, completion: { finished in
  print("Done")
})
B
UIView.animate(withDuration: 0.5, animations: {
  view.alpha = 0
}, completion: {
  print("Done")
})
C
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: [], animations: {
  view.alpha = 0
}, completion: { finished in
  print("Done")
})
D
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, animations: {
  view.alpha = 0
})
Attempts:
2 left
💡 Hint
Check the method signature for spring animations with completion.
lifecycle
advanced
2:00remaining
What happens if you start a spring animation on a view that is not yet in the view hierarchy?
You create a UIView and start a spring animation on it before adding it to the visible view hierarchy. What is the expected behavior?
AThe animation runs but is invisible until the view is added, then it jumps to the final state.
BThe app crashes due to invalid animation on a non-visible view.
CThe animation runs normally and the view animates when added later.
DThe animation does not run and the view appears instantly at the final state when added.
Attempts:
2 left
💡 Hint
Animations require the view to be in the window to be visible.
navigation
advanced
2:00remaining
How to chain two spring animations so the second starts after the first completes?
You want to animate a view moving right with a spring animation, then after it finishes, animate it moving down with another spring animation. Which code snippet achieves this chaining correctly?
A
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
  view.frame.origin.x += 100
}, completion: { _ in
  UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
    view.frame.origin.y += 100
  }, completion: nil)
})
B
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
  view.frame.origin.x += 100
})
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
  view.frame.origin.y += 100
})
C
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
  view.frame.origin.x += 100
}, completion: nil)
UIView.animate(withDuration: 0.5, delay: 0.5, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
  view.frame.origin.y += 100
}, completion: nil)
D
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
  view.frame.origin.x += 100
}, completion: { _ in
  UIView.animate(withDuration: 0.5, delay: 0.5, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
    view.frame.origin.y += 100
  }, completion: nil)
})
Attempts:
2 left
💡 Hint
Use the completion block of the first animation to start the second.
🔧 Debug
expert
2:00remaining
Why does this spring animation not bounce as expected?
This code is intended to animate a view with a spring effect that bounces, but it moves smoothly without bounce. What is the cause?
iOS Swift
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
  view.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}, completion: nil)
AThe damping value is too high, preventing any bounce effect.
BThe initialSpringVelocity is zero, so no bounce occurs.
CThe animation duration is too long for bounce to be visible.
DThe transform property cannot be animated with spring effects.
Attempts:
2 left
💡 Hint
Damping controls how much the animation oscillates.