Bird
Raised Fist0
iOS Swiftmobile~20 mins

Custom animation timing in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Custom Animation Timing
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 animation code?
Consider this Swift code snippet for a UIView animation with a custom timing curve. What will the user see when this animation runs?
iOS Swift
UIView.animate(withDuration: 2.0, delay: 0, options: [.curveEaseInOut], animations: {
  view.alpha = 0
}, completion: nil)
AThe view fades out instantly without any animation.
BThe view fades out at a constant speed over 2 seconds.
CThe view fades out quickly at first, then slows down towards the end.
DThe view fades out slowly at first, speeds up in the middle, then slows down before disappearing.
Attempts:
2 left
💡 Hint
Think about what the .curveEaseInOut option means for animation speed over time.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a custom timing curve animation?
You want to animate a view's position with a custom cubic timing curve. Which code snippet correctly uses UIViewPropertyAnimator with a custom timing curve?
A
let timing = UICubicTimingParameters(controlPoint1: CGPoint(x: 0.1, y: 0.9), controlPoint2: CGPoint(x: 0.2, y: 0.8))
let animator = UIViewPropertyAnimator(duration: 1.0, timingParameters: timing)
animator.addAnimations {
  view.frame.origin.x += 100
}
animator.startAnimation()
B
let animator = UIViewPropertyAnimator(duration: 1.0, timingParameters: UICubicTimingParameters(controlPoint1: CGPoint(x: 0.0, y: 0.0), controlPoint2: CGPoint(x: 1.0, y: 1.0)))
animator.addAnimations {
  view.frame.origin.x += 100
}
animator.startAnimation()
C
UIView.animate(withDuration: 1.0, delay: 0, options: [.curveLinear], animations: {
  view.frame.origin.x += 100
}, completion: nil)
D
let animator = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
  view.frame.origin.x += 100
}
animator.startAnimation()
Attempts:
2 left
💡 Hint
Look for the option that uses UICubicTimingParameters with control points.
❓ lifecycle
advanced
2:00remaining
What happens if you call startAnimation() twice on the same UIViewPropertyAnimator?
Given a UIViewPropertyAnimator instance, what is the effect of calling startAnimation() two times in a row without stopping or pausing?
iOS Swift
let animator = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
  view.alpha = 0
}
animator.startAnimation()
animator.startAnimation()
AThe second call has no effect because the animation is already running.
BThe animation runs twice sequentially, fading out the view two times.
CThe animation restarts from the beginning on the second call.
DThe app crashes due to invalid animation state.
Attempts:
2 left
💡 Hint
Think about how UIViewPropertyAnimator manages its running state.
❓ navigation
advanced
2:00remaining
How to synchronize a custom animation with a navigation controller transition?
You want to add a custom timing animation to a view during a UINavigationController push transition. Which approach correctly synchronizes your animation with the navigation transition?
AUse UIView.animate inside viewWillAppear of the pushed view controller without any coordination.
BStart your animation in viewDidLoad of the pushed view controller with a fixed delay matching the navigation animation duration.
CImplement UIViewControllerAnimatedTransitioning and use the transitionContext to run your custom animation synchronized with the navigation transition.
DAdd your animation in viewDidAppear of the pushed view controller after the navigation transition completes.
Attempts:
2 left
💡 Hint
Custom navigation animations require coordination with the transition system.
🔧 Debug
expert
2:00remaining
Why does this custom timing animation freeze halfway?
This code uses UIViewPropertyAnimator with a custom timing curve, but the animation freezes halfway and never completes. What is the most likely cause?
iOS Swift
let timing = UICubicTimingParameters(controlPoint1: CGPoint(x: 1.5, y: -0.5), controlPoint2: CGPoint(x: 0.5, y: 1.5))
let animator = UIViewPropertyAnimator(duration: 2.0, timingParameters: timing)
animator.addAnimations {
  view.alpha = 0
}
animator.startAnimation()
AThe view's alpha property cannot be animated with a custom timing curve.
BThe control points are outside the valid range, causing the animation to behave unpredictably and freeze.
CThe animation duration is too long, causing it to appear frozen.
DThe animator was not retained and got deallocated immediately.
Attempts:
2 left
💡 Hint
Check the valid range for cubic Bezier control points in timing curves.

Practice

(1/5)
1. What does the .curveEaseIn animation option do in UIView animations?
easy
A. Starts the animation quickly and then slows down.
B. Starts the animation slowly and then speeds up.
C. Keeps the animation speed constant throughout.
D. Reverses the animation direction automatically.

Solution

  1. Step 1: Understand UIView animation curves

    UIView animation curves control how the animation speed changes over time.
  2. Step 2: Identify the effect of .curveEaseIn

    .curveEaseIn means the animation starts slowly and accelerates towards the end.
  3. Final Answer:

    Starts the animation slowly and then speeds up. -> Option B
  4. Quick Check:

    .curveEaseIn = slow start, speed up [OK]
Hint: EaseIn means slow start, speed up later [OK]
Common Mistakes:
  • Confusing .curveEaseIn with .curveEaseOut
  • Thinking it keeps constant speed
  • Assuming it reverses animation
2. Which of the following is the correct syntax to animate a view's alpha to 0.5 over 0.3 seconds with ease-out timing?
easy
A. UIView.animate(duration: 0.3, options: .easeOut) { view.alpha = 0.5 }
B. UIView.animate(withDuration: 0.3, options: .curveEaseIn) { view.alpha = 0.5 }
C. UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) { view.alpha = 0.5 }
D. UIView.animate(withDuration: 0.3) { view.alpha = 0.5, options: .curveEaseOut }

Solution

  1. Step 1: Recall UIView.animate syntax

    The correct method signature is UIView.animate(withDuration:delay:options:animations:completion:). Options must be passed as a parameter.
  2. Step 2: Check each option for syntax correctness

    UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) { view.alpha = 0.5 } uses correct method name, parameters, and .curveEaseOut option. Others have wrong method names or misplaced options.
  3. Final Answer:

    UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) { view.alpha = 0.5 } -> Option C
  4. Quick Check:

    Correct UIView.animate syntax = UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) { view.alpha = 0.5 } [OK]
Hint: Use withDuration and options parameters correctly [OK]
Common Mistakes:
  • Using wrong method name like animate(duration:)
  • Placing options inside animation block
  • Forgetting delay parameter
3. What will be the visible effect of this code snippet?
UIView.animate(withDuration: 1.0, delay: 0, options: [.curveEaseInOut]) {
  view.frame.origin.x += 100
}
medium
A. The view moves 100 points to the right, starting fast and slowing down.
B. The view moves 100 points to the right at a constant speed.
C. The view jumps instantly 100 points to the right without animation.
D. The view moves 100 points to the right, starting slow, speeding up, then slowing down.

Solution

  1. Step 1: Understand .curveEaseInOut timing

    .curveEaseInOut means the animation starts slowly, speeds up in the middle, then slows down at the end.
  2. Step 2: Analyze the animation effect

    The view's x position increases by 100 points over 1 second with ease-in-out timing, so it moves smoothly with acceleration and deceleration.
  3. Final Answer:

    The view moves 100 points to the right, starting slow, speeding up, then slowing down. -> Option D
  4. Quick Check:

    .curveEaseInOut = slow start & end, fast middle [OK]
Hint: EaseInOut means slow start and end, fast middle [OK]
Common Mistakes:
  • Thinking it moves at constant speed
  • Confusing easeInOut with easeIn or easeOut
  • Expecting instant jump without animation
4. Identify the error in this Core Animation timing code:
let animation = CABasicAnimation(keyPath: "opacity")
animation.duration = 1.0
animation.timingFunction = CAMediaTimingFunction(name: .easeIn)
animation.fromValue = 1.0
animation.toValue = 0.0
view.layer.add(animation, forKey: "fade")
medium
A. The animation is missing a fillMode to keep final state.
B. The timingFunction property expects a string, not CAMediaTimingFunction.
C. The keyPath "opacity" is invalid for CABasicAnimation.
D. The timing function name should be .easeInEaseOut, not .easeIn.

Solution

  1. Step 1: Check timingFunction usage

    Using CAMediaTimingFunction(name: .easeIn) is correct and supported.
  2. Step 2: Identify missing fillMode

    Without setting fillMode and isRemovedOnCompletion, the animation resets after finishing, so the view returns to original opacity.
  3. Final Answer:

    The animation is missing a fillMode to keep final state. -> Option A
  4. Quick Check:

    Missing fillMode causes animation to revert [OK]
Hint: Add fillMode and isRemovedOnCompletion to keep final state [OK]
Common Mistakes:
  • Assuming .easeIn is invalid timing function name
  • Thinking timingFunction needs a string
  • Ignoring fillMode and completion behavior
5. You want to create a custom animation that moves a view along a path with a non-linear speed, speeding up then slowing down repeatedly. Which approach best achieves this?
hard
A. Use CAKeyframeAnimation with a custom timing function and path to control speed and position.
B. Use CABasicAnimation with a custom CAMediaTimingFunction using control points for the speed curve.
C. Use UIView.animate with linear curve and manually update position in a timer.
D. Use UIView.animate with .curveEaseInOut option repeatedly in a loop.

Solution

  1. Step 1: Understand animation requirements

    The animation needs to follow a path and have complex speed changes (speeding up and slowing down repeatedly).
  2. Step 2: Evaluate animation options

    CAKeyframeAnimation allows animating along a path and supports custom timing functions for complex speed control, making it ideal here.
  3. Final Answer:

    Use CAKeyframeAnimation with a custom timing function and path to control speed and position. -> Option A
  4. Quick Check:

    CAKeyframeAnimation + custom timing = best for complex path and speed [OK]
Hint: Use CAKeyframeAnimation for path + custom speed control [OK]
Common Mistakes:
  • Using UIView.animate for complex path animations
  • Assuming CABasicAnimation can follow a path
  • Trying to manually update position with timers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard