iOS Animations(UIView)

Method

+ (void)animateWithDuration:(NSTimeInterval)duration
                      delay:(NSTimeInterval)delay
                    options:(UIViewAnimationOptions)options
                 animations:(void (^)(void))animations
                 completion:(void (^ __nullable)(BOOL finished))completion

+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations
                 completion:(void (^ __nullable)(BOOL finished))completion

+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations

duration:持續(xù)時(shí)間
delay:延遲時(shí)間
options:選項(xiàng),詳細(xì)看下表
animations:動(dòng)畫block
completion:完成block

Animatable properties

Position and size Appearance Transformation
bounds backgroundColor transform
frame alpha
center

Springs

+ (void)animateWithDuration:(NSTimeInterval)duration
                      delay:(NSTimeInterval)delay
     usingSpringWithDamping:(CGFloat)dampingRatio
      initialSpringVelocity:(CGFloat)velocity
                    options:(UIViewAnimationOptions)options
                 animations:(void (^)(void))animations
                 completion:(void (^ __nullable)(BOOL finished))completion

dampingRatio:阻尼系數(shù)(0~1)
velocity:初始速度

Spring.gif

Transitions

+ (void)transitionWithView:(UIView *)view
                  duration:(NSTimeInterval)duration
                   options:(UIViewAnimationOptions)options
                animations:(void (^ __nullable)(void))animations
                completion:(void (^ __nullable)(BOOL finished))completion

+ (void)transitionFromView:(UIView *)fromView
                    toView:(UIView *)toView
                  duration:(NSTimeInterval)duration
                   options:(UIViewAnimationOptions)options
                completion:(void (^ __nullable)(BOOL finished))completion
// toView added to fromView.superview, fromView removed from its superview
Transitions.gif

UIViewAnimationOptions

UIViewAnimationOptionLayoutSubviews             動(dòng)畫過程中保證子視圖跟隨運(yùn)動(dòng)
UIViewAnimationOptionAllowUserInteraction       允許交互
UIViewAnimationOptionBeginFromCurrentState      從當(dāng)前狀態(tài)開始運(yùn)行
UIViewAnimationOptionRepeat                     重復(fù)
UIViewAnimationOptionAutoreverse                逆動(dòng)畫
UIViewAnimationOptionOverrideInheritedDuration  忽略嵌套動(dòng)畫時(shí)間設(shè)置
UIViewAnimationOptionOverrideInheritedCurve     忽略嵌套動(dòng)畫速度設(shè)置
UIViewAnimationOptionAllowAnimatedContent       動(dòng)畫過程中重繪視圖(僅適用轉(zhuǎn)場)
UIViewAnimationOptionShowHideTransitionViews    轉(zhuǎn)場隱藏舊視圖
UIViewAnimationOptionOverrideInheritedOptions   不繼承父動(dòng)畫設(shè)置
    
UIViewAnimationOptionCurveEaseInOut             先慢后快再慢
UIViewAnimationOptionCurveEaseIn                逐漸變慢
UIViewAnimationOptionCurveEaseOut               逐漸變快
UIViewAnimationOptionCurveLinear                線性運(yùn)動(dòng)

UIViewAnimationOptionTransitionNone             無效果
UIViewAnimationOptionTransitionFlipFromLeft     繞X軸逆時(shí)針旋轉(zhuǎn)
UIViewAnimationOptionTransitionFlipFromRight    繞X軸順時(shí)針旋轉(zhuǎn)
UIViewAnimationOptionTransitionCurlUp           從右下角掀起
UIViewAnimationOptionTransitionCurlDown         從右下角落下
UIViewAnimationOptionTransitionCrossDissolve    溶解
UIViewAnimationOptionTransitionFlipFromTop      從上方翻轉(zhuǎn)
UIViewAnimationOptionTransitionFlipFromBottom   從下方翻轉(zhuǎn)
options.gif

Keyframe Animations

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
                               delay:(NSTimeInterval)delay
                             options:(UIViewKeyframeAnimationOptions)options
                          animations:(void (^)(void))animations
                          completion:(void (^ __nullable)(BOOL finished))completion
    
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
                        relativeDuration:(double)frameDuration
                              animations:(void (^)(void))animations

frameStartTime:起始時(shí)間點(diǎn)(0~1)
frameDuration:持續(xù)時(shí)間(0~1)

Demo

[UIView animateKeyframesWithDuration:5.0f
                               delay:0.0f
                             options:UIViewKeyframeAnimationOptionCalculationModeLinear
                          animations:^{
                              [UIView addKeyframeWithRelativeStartTime:0.0f
                                                      relativeDuration:0.3f
                                                            animations:^{
                                                                _testView.y += 100;
                                                            }];
                              [UIView addKeyframeWithRelativeStartTime:0.3f
                                                      relativeDuration:0.7f
                                                            animations:^{
                                                                _testView.x += 50;
                                                            }];
                          }
                          completion:nil];

UIViewKeyframeAnimationOptions

UIViewKeyframeAnimationOptionLayoutSubviews
UIViewKeyframeAnimationOptionAllowUserInteraction
UIViewKeyframeAnimationOptionBeginFromCurrentState
UIViewKeyframeAnimationOptionRepeat
UIViewKeyframeAnimationOptionAutoreverse
UIViewKeyframeAnimationOptionOverrideInheritedDurationnested
UIViewKeyframeAnimationOptionOverrideInheritedOptions

UIViewKeyframeAnimationOptionCalculationModeLinear     連續(xù)運(yùn)行模式
UIViewKeyframeAnimationOptionCalculationModeDiscrete   離散運(yùn)行模式
UIViewKeyframeAnimationOptionCalculationModePaced      均勻執(zhí)行運(yùn)行模式
UIViewKeyframeAnimationOptionCalculationModeCubic      平滑運(yùn)行模式
UIViewKeyframeAnimationOptionCalculationModeCubicPaced 平滑均勻運(yùn)行模式
UIViewKeyframeAnimationOptions.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容