Kinds of Animation
-
Animating UIView properties
Changing things like the frame or transparency.
-
Animating Controller transitions(as in a UINavigationController)
-
Core Animation
Underlying powerful animation framework
-
OpenGL and Metal
3D
-
SpriteKit
"2.5D"animation (overlapping images moving around over each other , etc.)
-
Dynamic Animation
"Physics"-based animation
UIView Animation
Changes to certain UIView properties can be animated over time
-
Frame/center
-
Transform (translation,rotation and scale)
-
alpha(opactiy)
-
Background color
Done with UIView class method(s) using closures
The calss methods takes animation parameters and an animation block as arguments.
The animation block contains the code that makes the changes to the UIView(s).
The changes inside the block are made immediately (even though they will appear over time)
Most also have another "completion block" to be executed when the animation is done.
Example
if myView.alpha == 1.0 {
UIView.animate(withDuration:3.0,
delay:2.0,
options:[.curveLinear],
animations:{myView.alpha = 0.0},
completion:{if $0 {myView.removeFromSuperView()}})
print("myView.alpha=\(myView.alpha)")
}
This would cause myView to fade out over 3 seconds (starting 2s form now).
Then it would remove myView form the view hierarchy(but only if the fade completed)
If,within the 5s, someone animated the alpha to non-zero, the removal would note hanppen.
The output on the console would be ...
myView.alpha = 0.0
Even though the alpha on the screen won't be zero for 5 more seconds
UIViewAnimationOptions
beginFromCurrentState //pick up form other,in-progress animations of the these properties
allowUserInteraction//allow gestrues to get processed while animation is in progeress
layoutSubviews // animate the relayout of subviews with a parent's animation
repeat// repeat indefinitely
autoreverse// play animation forwards,then backwords
ovverrideInheritedDuration// if not set ,use duration of any in-progress animation
ovverrideInhertiedCurve// if not set,use curve of in-progress animation
allowAnimatedContent// if not set, just interpolate between current and end "bits"
curveEaseInEaseOut // slower at the beginning ,normal throughout,then slow at end
curveEaseIn// slower at the beginning,but then constant through the rest
curveLinear// same speed throughout
make an entire view modification at once
In this case you are not limited to special properties like alpha,frame and thransform
Flip the entire view over UIViewAnimationOptions.transitionFlipFrom{Left,Right,Top,Bottom}
Dissolve from old to new state .transitionCrossDissolve
Curling up or down .transitionCurl{Up,Down}
// use UIView.transisition(with
UIView.transition(with:UIView,
duration:TimeInterval,
options:UIViewAnimationOptions,
animations:()->Void,
completion:((finished:Bool)->Void)?)
animating changes to the view hierarchy
In other words, you want to animate the adding/removing of subviews(or (un)hiding them)
// use UIView.transition(from..to..
UIView.transition(from:UIView,
to:UIView,
duration:TimeInterval,
options:UIViewAnimationOptions,
completion:((finished:Bool)->Void)?)
UIViewAnimationOptions.showHideTransitionViews if you want to use the hidden property.
Otherwise it will actually remove fromView form the view hierarchy and add toView
网友评论