iOS - Animation之ViewAnimation

這里不僅有present和dismiss在viewController中跳轉(zhuǎn)捶码,還有各種動(dòng)畫push和pop,當(dāng)然view的動(dòng)畫出場也是必不可少的或链。拿了直接用即可惫恼,簡單粗暴看代碼,這里可以讓你知其然澳盐,知其所以然祈纯。

PresentAnimation

實(shí)現(xiàn)UIViewControllerTransitioningDelegate的代理方法,把present從下面出來的view從右邊動(dòng)畫出來叼耙,具體代碼看github代碼腕窥,具體方法如下:

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 1.0;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
 UIViewController * fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController * toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView =  [transitionContext containerView];
UIView *fromView = nil;
UIView *toView = nil;

if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
    fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    toView = [transitionContext viewForKey:UITransitionContextToViewKey];
}else{
    fromView = fromViewController.view;
    toView = toViewController.view;
}
BOOL isPresent = (toViewController.presentingViewController == fromViewController);
CGRect fromFrame = [transitionContext initialFrameForViewController:fromViewController];
CGRect toFrame = [transitionContext finalFrameForViewController:toViewController];
if (isPresent) {
    fromView.frame = fromFrame;
    toView.frame = CGRectOffset(toFrame, toFrame.size.width, 0);
    [containerView addSubview:toView];
}  
NSTimeInterval transitionDuration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:transitionDuration animations:^{
    if (isPresent) {
        toView.frame = toFrame;
        fromView.frame = CGRectOffset(fromFrame, fromFrame.size.width*0.3*-1, 0);
    }
} completion:^(BOOL finished) {
    BOOL isCancelled = [transitionContext transitionWasCancelled];
    if (isCancelled)
        [toView removeFromSuperview]; 
    [transitionContext completeTransition:!isCancelled];
}];

}

  • (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
return (id)[[PresentAnimation alloc]init];
}

DismissAnimation

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 1.0;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

UIView *containerView = [transitionContext containerView];
UIView *fromView = nil;
UIView *toView = nil;

if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
    fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    toView = [transitionContext viewForKey:UITransitionContextToViewKey];
}else{
    fromView = fromViewController.view;
    toView = toViewController.view;
}

BOOL isDismiss = (fromViewController.presentingViewController == toViewController);   
CGRect fromFrame = [transitionContext initialFrameForViewController:fromViewController];
CGRect toFrame = [transitionContext finalFrameForViewController:toViewController];

if (isDismiss) {
    fromView.frame = fromFrame;
    toView.frame = CGRectOffset(toFrame, toFrame.size.width*0.3*-1, 0);
    [containerView insertSubview:toView belowSubview:fromView];
}
NSTimeInterval transitionDuration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:transitionDuration animations:^{
    if (isDismiss) {
        toView.frame = toFrame;
        fromView.frame = CGRectOffset(fromFrame, fromFrame.size.width, 0);
    }
} completion:^(BOOL finished) {
    BOOL isCancel = [transitionContext transitionWasCancelled];
    if (isCancel) {
        [toView removeFromSuperview];
    }
    [transitionContext completeTransition:!isCancel];
}];
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
return (id)[[DismissAnimation alloc]init];
  }

PushAnimation

動(dòng)畫添加新的view,type和subType不同筛婉,動(dòng)畫方式就不一樣簇爆。這里只寫一個(gè)不累贅

 +(void)pushView:(UIView *)View subView:(UIView *)subView AndAnimationType:(NSString *)type AndsubType:(NSString *)subType{  
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = type;
transition.subtype = subType;
[View.layer addAnimation:transition forKey:@"pushTransitionAnimation"];
[View addSubview:subView];
  }

PopAnimation

動(dòng)畫移除新的view

 +(void)popView:(UIView *)View subView:(UIView *)subView AndAnimationType:(NSString *)type AndsubType:(NSString *)subType {
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = type;
transition.subtype = subType;
[View.superview.layer addAnimation:transition forKey:@"popTransitionAnimation"];
[subView removeFromSuperview];
 }

注:關(guān)于CATransition的type和subType的介紹,前兩篇博客已經(jīng)介紹過,看Animation_CATransition這篇入蛆。

ScaleViewAnimation

CGAffineTransform 主要用于形變响蓉,位移和旋轉(zhuǎn),可用于動(dòng)畫展示

transform屬性介紹哨毁,這里介紹最常用和好用的枫甲,其他屬性去看CGAffineTransform
  • 位移X,Y

      CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)
    
  • 伸縮X,Y

      CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
    
  • 旋轉(zhuǎn)M_PI

      CGAffineTransformMakeRotation(CGFloat angle)
    
  • 組合

      CGAffineTransformConcat(CGAffineTransform t1,CGAffineTransform t2)
    
  • 回到原位

      CGAffineTransformIdentity
    
動(dòng)畫調(diào)用,包括動(dòng)畫時(shí)間扼褪,動(dòng)畫完成后回調(diào)
 -(void)scaleView:(UIView *)view CGFloatX:(CGFloat)x CGFloatY:(CGFloat)y CGFloatW:(CGFloat)w CGFloatH:(CGFloat)h{
CGFloat scaleW = self.window.frame.size.width / w;
CGFloat scaleH = self.window.frame.size.height / h;
CGFloat W = scaleW * 50 - 50 - x;
CGFloat H = scaleH * 50 - 50 - y;
[UIView animateWithDuration:1.0 animations:^{
    view.transform =CGAffineTransformConcat(CGAffineTransformMakeScale(scaleW, scaleH), CGAffineTransformMakeTranslation(W,H));
} completion:^(BOOL finished) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        view.transform = CGAffineTransformIdentity;
    });
  }];

}

Dome: github地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末言秸,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子迎捺,更是在濱河造成了極大的恐慌,老刑警劉巖查排,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件凳枝,死亡現(xiàn)場離奇詭異,居然都是意外死亡跋核,警方通過查閱死者的電腦和手機(jī)岖瑰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來砂代,“玉大人蹋订,你說我怎么就攤上這事】桃粒” “怎么了露戒?”我有些...
    開封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長捶箱。 經(jīng)常有香客問我智什,道長,這世上最難降的妖魔是什么丁屎? 我笑而不...
    開封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任荠锭,我火速辦了婚禮,結(jié)果婚禮上晨川,老公的妹妹穿的比我還像新娘证九。我一直安慰自己,他們只是感情好共虑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開白布愧怜。 她就那樣靜靜地躺著,像睡著了一般看蚜。 火紅的嫁衣襯著肌膚如雪叫搁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音渴逻,去河邊找鬼疾党。 笑死,一個(gè)胖子當(dāng)著我的面吹牛惨奕,可吹牛的內(nèi)容都是我干的雪位。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼梨撞,長吁一口氣:“原來是場噩夢啊……” “哼雹洗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起卧波,我...
    開封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤时肿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后港粱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體螃成,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年查坪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了寸宏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡偿曙,死狀恐怖氮凝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情望忆,我是刑警寧澤罩阵,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站炭臭,受9級(jí)特大地震影響永脓,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鞋仍,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一常摧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧威创,春花似錦落午、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至吸申,卻和暖如春梗劫,著一層夾襖步出監(jiān)牢的瞬間享甸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來泰國打工梳侨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蛉威,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓走哺,卻偏偏與公主長得像蚯嫌,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子丙躏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容