Custom animation timing lets you control how fast or slow an animation plays at different moments. This makes animations feel smooth and natural.
Custom animation timing in iOS Swift
UIView.animate(withDuration: TimeInterval, delay: TimeInterval, options: UIView.AnimationOptions, animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil)
The options parameter lets you set timing curves like .curveEaseIn, .curveEaseOut, or .curveLinear.
For more custom control, use CAMediaTimingFunction with Core Animation layers.
UIView.animate(withDuration: 1.0, animations: { view.alpha = 0 })
UIView.animate(withDuration: 1.0, delay: 0, options: .curveEaseIn, animations: { view.frame.origin.y += 100 }, completion: nil)
UIView.animate(withDuration: 1.0, delay: 0, options: .curveEaseOut, animations: { view.transform = CGAffineTransform(scaleX: 1.5, y: 1.5) }, completion: nil)
This code creates a blue square and moves it down while fading it out. The animation speeds up at the start and slows down at the end for a smooth effect.
import UIKit class ViewController: UIViewController { let box = UIView() override func viewDidLoad() { super.viewDidLoad() box.frame = CGRect(x: 100, y: 100, width: 100, height: 100) box.backgroundColor = .systemBlue view.addSubview(box) // Animate with custom timing curve UIView.animate(withDuration: 2.0, delay: 0, options: .curveEaseInOut, animations: { self.box.frame.origin.y += 200 self.box.alpha = 0.5 }, completion: nil) } }
Use .curveEaseInOut for smooth start and end.
For very custom timing, explore CAMediaTimingFunction and CAKeyframeAnimation.
Remember to test animations on real devices for best feel.
Custom animation timing controls how animation speed changes over time.
Use UIView animation options like .curveEaseIn and .curveEaseOut for simple timing changes.
For advanced control, use Core Animation timing functions.
Practice
.curveEaseIn animation option do in UIView animations?Solution
Step 1: Understand UIView animation curves
UIView animation curves control how the animation speed changes over time.Step 2: Identify the effect of .curveEaseIn
.curveEaseIn means the animation starts slowly and accelerates towards the end.Final Answer:
Starts the animation slowly and then speeds up. -> Option BQuick Check:
.curveEaseIn = slow start, speed up [OK]
- Confusing .curveEaseIn with .curveEaseOut
- Thinking it keeps constant speed
- Assuming it reverses animation
Solution
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.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.Final Answer:
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) { view.alpha = 0.5 } -> Option CQuick Check:
Correct UIView.animate syntax = UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) { view.alpha = 0.5 } [OK]
- Using wrong method name like animate(duration:)
- Placing options inside animation block
- Forgetting delay parameter
UIView.animate(withDuration: 1.0, delay: 0, options: [.curveEaseInOut]) {
view.frame.origin.x += 100
}Solution
Step 1: Understand .curveEaseInOut timing
.curveEaseInOut means the animation starts slowly, speeds up in the middle, then slows down at the end.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.Final Answer:
The view moves 100 points to the right, starting slow, speeding up, then slowing down. -> Option DQuick Check:
.curveEaseInOut = slow start & end, fast middle [OK]
- Thinking it moves at constant speed
- Confusing easeInOut with easeIn or easeOut
- Expecting instant jump without animation
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")
Solution
Step 1: Check timingFunction usage
Using CAMediaTimingFunction(name: .easeIn) is correct and supported.Step 2: Identify missing fillMode
Without setting fillMode and isRemovedOnCompletion, the animation resets after finishing, so the view returns to original opacity.Final Answer:
The animation is missing a fillMode to keep final state. -> Option AQuick Check:
Missing fillMode causes animation to revert [OK]
- Assuming .easeIn is invalid timing function name
- Thinking timingFunction needs a string
- Ignoring fillMode and completion behavior
Solution
Step 1: Understand animation requirements
The animation needs to follow a path and have complex speed changes (speeding up and slowing down repeatedly).Step 2: Evaluate animation options
CAKeyframeAnimation allows animating along a path and supports custom timing functions for complex speed control, making it ideal here.Final Answer:
Use CAKeyframeAnimation with a custom timing function and path to control speed and position. -> Option AQuick Check:
CAKeyframeAnimation + custom timing = best for complex path and speed [OK]
- Using UIView.animate for complex path animations
- Assuming CABasicAnimation can follow a path
- Trying to manually update position with timers
Ready to prove it?
Pass the test and this pattern moves unknown
