-
Notifications
You must be signed in to change notification settings - Fork 781
How to develop an animator (animation controller)
To support custom transition animation, we need to develop animator (animation controller). It is every easy to develop animators.
-
Define an animation or a set of animations in
TransitionAnimationType
, e.g.Fade
,FadeIn
-
Add parsing logic in
static func fromString(transitionType: String) -> TransitionAnimationType?
inTransitionAnimationType
to support the new transition animation type. -
Create an animator class under "IBAnimatable" -> "animator" -> "transition animator"
-
Name it as
***Animator
likeFadeAnimator
-
Inherit from
NSObject
and confirm toAnimatedTransitioning
likepublic class FadeAnimator: NSObject , AnimatedTransitioning
-
Override properties, if it has a different transition duration, specify it like
transitionDuration = 0.8
, otherwise, set it asdefaultTransitionDuration
. If this animation supports gesture to dismiss or pop, specify it ininteractiveGestureType
, otherwise, let it blank.
// MARK: - AnimatorProtocol
public var transitionAnimationType: TransitionAnimationType
public var transitionDuration: Duration = defaultTransitionDuration
public var reverseAnimationType: TransitionAnimationType?
public var interactiveGestureType: InteractiveGestureType? = .PanHorizontally
-
Implement
init
method to specify thereverseAnimationType
if it has one. -
Use protocol extension to override two methods. like
extension FadeAnimator: UIViewControllerAnimatedTransitioning
-
For
transitionDuration
, we can just use
public func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return retrieveTransitionDuration(transitionContext)
}
-
Implement
animateTransition
, it is the creative bit, we always implement this method differently to support different animations, we will spend most of time on this. -
Add new switch case(s) in
AnimatorFactory
to support new animation(s). -
Add new animation(s) to the demo app in
func generateTransitionTypeData()
inTransitionTableViewController
.
transitionAnimationsHeaders.append("SystemFlip")
transitionAnimations.append(transitionTypeWithDirections(forName: "SystemFlip"))
transitionAnimationsHeaders.append("SystemPageCurl")
transitionAnimations.append(["SystemPageCurl(Top)", "SystemPageCurl(Bottom)"])
- Now, we should be able to see the new animations in the demo app, just tap on "Forgot password". And test it carefully.
-
If you are happy with the animation. Create segue(s) for it. e.g.
PresentFadeSegue
andPresentFadeInSegue
to support segue for present and dismiss transitions. -
If the animation can support gesture to dismiss, create segue(s) for it with interaction support. e.g.
PresentFadeWithDismissInteractionSegue
andPresentFadeInWithDismissInteractionSegue
FadeAnimator is a good example we can follow.
If you have any questions, please let me know.
Have fun!!! 👯👯👯