قالب وردپرس درنا توس
Home / IOS Development / Quick Guide: Animations with UIViewPropertyAnimator

Quick Guide: Animations with UIViewPropertyAnimator



With iOS 10, a number of new interesting features, such as UIViewPropertyAnimator brought a whole new class that enhances animation management.
The display feature completely animates the current we are using, adding a finer control over the animation logic.

An Easy Animation

Let's see how to build a simple animation to change the center attribute of a view.

  la animator = UIViewPropertyAnimator (duration: 1.0, curve: .easeOut) {
AView.center = finalpoint
}
animator.startAnimation ()

There are at least three interesting things to note:
1) Animation is defined by a closure, really similar to the UIView animation aids "UIView.animation (duration: …)".
2) An object, the animator, is returned.
3) The animation does not start immediately but is triggered by the start animation () function.

Animation Mode

The major changes in the way we animate an element are related to the fact that with a real estate animator comes a state-owned machine logic. Through the protocol UIViewAnimating features are implemented to manage the state of the animation in a simple and clear manner, implemented by features such as start animation pause animation and stop animation . Call the features we update the state value so that it switches between active inactive and stopped .

Animation mode is active when the animation is started or paused, it is idle when it is only initialized and not started or when it is completed. It is better to clarify that there is a slight difference between inactive and stopped. When the animation completes after a stop command or completes, the state stops, internally called the animators feature finish animation (at 🙂 to mark the animation as complete, set the state as idle and finally call completion block (more later) .

Animation options

As you have probably noticed with the previous example, we have, along with the animation block, defined two parameters: duration of the animation and animation curve one UIViewAnimationCurve example that can represent the most common curves (easeIn, easeOut, linear or easyInOut).

If you need more control over the animation curve, you can use a custom viewer curve defined by 2 checkpoints.

  la animator = UIViewPropertyAnimator (
duration: 1.0,
Point 1: CGPoint (0.1.0.5),
point2: CGPoint (0.5, 0.2) {

AView.alpha = 0.0
}

(If the bezier curves are not enough, you can even specify a fully customized curve with an UITimigCurveProvider)

Another interesting alternative that you can transfer to the constructor is dempingRatio ] ] value. In the same way as UIView animation aids, you can define a feather effect that sets a damping value from 0 to 1.

  la animator = UIViewPropertyAnimator (UIViewPropertyAnimator
duration: 1.0,
vapor Ratio: 0.4) {

AView.center = CGPoint (x: 0, y: 0)
}

Delay the animation is simply too, just call the start animation feature with afterDelay param.

  animator.startAnimation (afterDelay: 2.5)

Animation Blocks

UIViewPropertyAnimator adopts UIViewImplicitlyAnimating the protocol that gives the animator some other interesting capabilities. For example, you can specify more animation blocks in addition to the first one specified during initialization.

  // Initialization
la animator = UIViewPropertyAnimator (duration: 2.0, curve: .easeOut) {
AView.alpha = 0.0
}
// Another animation block
animator.addAnimation {
Aview.center = aNewPosition
}
animator.startAnimation ()

You can also add animations blocked to an already running animation, the block will be executed immediately with the remaining time as the duration of the new animation.

Interacting with the Animation Flow

As we have already mentioned, we can easily interact with the animation flow screen start animation, stop animation and pause animation. The default flow of the animation, from start to finish, can be changed through the fraction completions feature . This value specifies the percentage of completion of the animation with a value ranging from 0.0 to 1.0. You can change the value to drive the current as you prefer (example: the user can change the fraction in real time using a slider or board).

  animator.fractionComplete = slider.value

In some cases, you may want to perform actions when the animation completes its run. The addCompletion feature allows you to add a block that will be triggered when the animation is completed.

  animator.addCompletion {(position) in
print ("Animation Completed")
}

The position is a UIViewAnimatingPosition and it indicates if the animation stopped a start, end, or current position. Normally you will receive the final value.

It's all for this quick guide.
I can not wait to play more with this new animation system to implement some really nice UI effects! I share my experiments on Twitter ;) Ciao!




Source link