iOS開發(fā)高級進階(8-10)-動畫

(還是沒搞懂。击你。辛臊。繼續(xù)看參考中)
交互設計的一部分:布局、變換

UIKit

UIView提供的動畫支持

  • (UIViewAnimation):depricated(過時)

  • UIView(UIViewAnimationWithBlocks)
    //修改animations參數蚪黑,可以實現(xiàn)動畫變換
    + (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 
    
    //Spring Animation, dampingRatio阻尼,velocity初速度
    + (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 ;
    
    + (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;
    
    + (void)performSystemAnimation:(UISystemAnimation)animation 
                    onViews:(NSArray<__kindof UIView *> *)views 
                    options:(UIViewAnimationOptions)options 
                    animations:(void (^ __nullable)(void))parallelAnimations 
                    completion:(void (^ __nullable)(BOOL finished))completion;
    

UIView可動畫的屬性

  • frame
  • bounds
  • center
  • transform
  • alpha
  • backgroundColor
  • contentStretch

動畫效果一覽

  typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
    UIViewAnimationOptionLayoutSubviews            = 1 <<  0,
    UIViewAnimationOptionAllowUserInteraction      = 1 <<  1, // turn on user interaction while animating
    UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2, // start all views from current value, not initial value
    UIViewAnimationOptionRepeat                    = 1 <<  3, // repeat animation indefinitely
    UIViewAnimationOptionAutoreverse               = 1 <<  4, // if repeat, run animation back and forth
    UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5, // ignore nested duration
    UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6, // ignore nested curve
    UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7, // animate contents (applies to transitions only)
    UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8, // flip to/from hidden state instead of adding/removing
    UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9, // do not inherit any options or animation type

    UIViewAnimationOptionCurveEaseInOut            = 0 << 16, // default
    UIViewAnimationOptionCurveEaseIn               = 1 << 16,
    UIViewAnimationOptionCurveEaseOut              = 2 << 16,
    UIViewAnimationOptionCurveLinear               = 3 << 16,

    UIViewAnimationOptionTransitionNone            = 0 << 20, // default
    UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
    UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
    UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
    UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
    UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
    UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
    UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,
} ;

UIView的Keyframe動畫(可控制中間狀態(tài))

+ (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; // start time and duration are values between 0.0 and 1.0 specifying time and duration relative to the overall time of the keyframe animation

Spring Animation

參考:https://www.renfei.org/blog/ios-8-spring-animation.html

Autolayout環(huán)境下的動畫

  1. 通過修改constraint實現(xiàn)
    2. 生效[self.view setNeedsUpdateConstraints];
  2. 在更改動畫塊
    [self.view layoutIfNeeded];

View Transition用動畫切換新界面

transitionWithView(子View)

[UIView transitionWithView:(nonnull UIView *)
                       duration:(NSTimeInterval)
                       options:(UIViewAnimationOptions)
                       animations:^(void)animations
                       completion:^(BOOL finished)completion];

//UIViewAnimationOptions
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
    UIViewAnimationOptionLayoutSubviews            = 1 <<  0,
    UIViewAnimationOptionAllowUserInteraction      = 1 <<  1, // turn on user interaction while animating
    UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2, // start all views from current value, not initial value
    UIViewAnimationOptionRepeat                    = 1 <<  3, // repeat animation indefinitely
    UIViewAnimationOptionAutoreverse               = 1 <<  4, // if repeat, run animation back and forth
    UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5, // ignore nested duration
    UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6, // ignore nested curve
    UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7, // animate contents (applies to transitions only)
    UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8, // flip to/from hidden state instead of adding/removing
    UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9, // do not inherit any options or animation type

    UIViewAnimationOptionCurveEaseInOut            = 0 << 16, // default
    UIViewAnimationOptionCurveEaseIn               = 1 << 16,
    UIViewAnimationOptionCurveEaseOut              = 2 << 16,
    UIViewAnimationOptionCurveLinear               = 3 << 16,

    UIViewAnimationOptionTransitionNone            = 0 << 20, // default
    UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
    UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
    UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
    UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
    UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
    UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
    UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,
} ;
//animations:block

視圖替換(場景變換較大)

[UIView transitionFromView:(nonnull UIView *)
                     toView:(nonnull UIView *)
                     duration:(NSTimeInterval) 
                     options:(UIViewAnimationOptions) 
                     completion:^(BOOL finished)completion];

Core Animation(操縱Layer形成動畫效果)

CATransaction

  • 對Layer屬性的修改都在CATransaction
  • 修改和生效的是異步的


    過程.png
進一步學習:
  • CATransaction +begin
    
  •  config current transation
    
  • CATransaction +commit
    

Implicit Animation(隱式動畫)

  • Core Animation默認是開著的
  • 修改layer的可動畫屬性時會自動觸發(fā)動畫

Layer可動畫的屬性

Layer可動畫的屬性.png

Todolist:一個一個試試

隱式動畫的查找

中剩?忌穿??什么用结啼?

CAAction

作用:禁止隱式動畫(讓-actionForKey找不到CAAction)
實現(xiàn):

//1.  return nil in delegate(CAAction) -actionForLayer:forKey; 
-(id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event{
    return  nil;
}
//2.
  [CATransaction setDisableActions:YES];

顯式動畫CAAnimation

接口:

1掠剑、CAAction
2、CAMediaTiming

子類:

  • CAPropertyAnimation
    • CABasicAnimation
    • CAKeyframeAnimation
  • CAAnimationGroup
  • CATransition

混合View及Layer動畫

<待補充>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末郊愧,一起剝皮案震驚了整個濱河市朴译,隨后出現(xiàn)的幾起案子井佑,更是在濱河造成了極大的恐慌,老刑警劉巖眠寿,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件躬翁,死亡現(xiàn)場離奇詭異,居然都是意外死亡盯拱,警方通過查閱死者的電腦和手機盒发,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來狡逢,“玉大人宁舰,你說我怎么就攤上這事∩莼耄” “怎么了蛮艰?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長雀彼。 經常有香客問我印荔,道長,這世上最難降的妖魔是什么详羡? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任仍律,我火速辦了婚禮,結果婚禮上实柠,老公的妹妹穿的比我還像新娘水泉。我一直安慰自己,他們只是感情好窒盐,可當我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布草则。 她就那樣靜靜地躺著,像睡著了一般蟹漓。 火紅的嫁衣襯著肌膚如雪炕横。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天葡粒,我揣著相機與錄音份殿,去河邊找鬼。 笑死嗽交,一個胖子當著我的面吹牛卿嘲,可吹牛的內容都是我干的。 我是一名探鬼主播夫壁,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼拾枣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起梅肤,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤司蔬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后姨蝴,有當地人在樹林里發(fā)現(xiàn)了一具尸體葱她,經...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年似扔,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片搓谆。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡炒辉,死狀恐怖,靈堂內的尸體忽然破棺而出泉手,到底是詐尸還是另有隱情黔寇,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布斩萌,位于F島的核電站缝裤,受9級特大地震影響,放射性物質發(fā)生泄漏颊郎。R本人自食惡果不足惜憋飞,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望姆吭。 院中可真熱鬧榛做,春花似錦、人聲如沸内狸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昆淡。三九已至锰瘸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間昂灵,已是汗流浹背避凝。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留眨补,地道東北人恕曲。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像渤涌,于是被迫代替她去往敵國和親佩谣。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內容