iOS 模態(tài)動畫3

效果:


11月-09-2017 10-22-43.gif

主要用到了物理引擎敢订。

新建動畫類MJAinimationRect
在頭文件中聲明下面兩個屬性,一個是動畫上下文,一個是物理引擎

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MJAnimationRect : NSObject<UIViewControllerAnimatedTransitioning,UICollisionBehaviorDelegate>
@property (strong , nonatomic) id < UIViewControllerContextTransitioning > transitionContext;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end

在實現(xiàn)文件中完成兩個代理相對應(yīng)該實現(xiàn)的代理方法

動畫過度時間孤荣,直接返回時間值

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 2.0f;
}

之后準備實現(xiàn)動畫效果,代碼如下
1.通過上下文獲取跳轉(zhuǎn)的view
2.將fromVC截圖
3.調(diào)用cutView方法將截圖切成4*5點格子并添加到當前的containerView上
4.添加之前隨機對切圖進行微角度旋轉(zhuǎn)须揣,以增加動畫效果
5.獲取目標view并添加到界面盐股,置于頁面底部,暫時先把他的alpha置為0
6.添加作用力
7.設(shè)置碰撞檢測

-(void)AnimationAnimationType:(id<UIViewControllerContextTransitioning>)transitionContext andToVC:(UIViewController*)toVC andFromVC:(UIViewController*)fromVC
{
    UIView *containerView = [transitionContext containerView];
    UIView *mainSnap = [fromVC.view snapshotViewAfterScreenUpdates:NO];
    NSMutableArray* cutViewArray = [self cutView:mainSnap intoSlicesOfWidth:mainSnap.frame.size.width/kwidthCount andHeight:mainSnap.frame.size.height/kHeightCount];
    for (int i=0; i<cutViewArray.count; i++) {
        UIView* cutView = (UIView*)cutViewArray[i];
        
        CGFloat angle = (i% 2 ? 1 : -1) * (rand() % 5 / 10.0);
        cutView.transform = CGAffineTransformMakeRotation(angle);
        [containerView addSubview:cutView];
    }
    UIView* toView = [toVC view];
    toView.frame = [transitionContext finalFrameForViewController:toVC];
    [containerView addSubview:toView];
    [containerView sendSubviewToBack:toView];
    toView.alpha = 0;
    
    //截圖已經(jīng)覆蓋fromView.view 所以可以直接隱藏返敬。
    fromVC.view.hidden = YES;

    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:containerView];
    UIDynamicBehavior *behaviour = [[UIDynamicBehavior alloc] init];
    //為每個view添加重力
    UIGravityBehavior* gravity = [[UIGravityBehavior alloc] initWithItems:cutViewArray];
    gravity.gravityDirection = CGVectorMake(0, 1); // 方向
    gravity.magnitude = 5; //加速度
    // 碰撞檢測
    UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:cutViewArray];
    // 設(shè)置不要出邊界遂庄,碰到邊界會被反彈
    collision.translatesReferenceBoundsIntoBoundary = YES;
    collision.collisionDelegate = self;
    //除了邊界碰撞沒有其他碰撞
    collision.collisionMode = UICollisionBehaviorModeBoundaries;
    [behaviour addChildBehavior:gravity];
    [behaviour addChildBehavior:collision];
    
    //每個view設(shè)置不同的動畫效果,添加到總的動作中去
    for (UIView *aView in cutViewArray) {
        UIDynamicItemBehavior *itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[aView]];
        itemBehaviour.elasticity = (rand() % 5) / 8.0;
        itemBehaviour.density = (rand() % 5 / 3.0);
                itemBehaviour.allowsRotation = YES;
        [behaviour addChildBehavior:itemBehaviour];
    }
    [self.animator addBehavior:behaviour];
    
    [UIView animateWithDuration:2 animations:^{
        for (UIView *aView in cutViewArray) {
            aView.alpha = 0.0;
            toView.alpha = 1;
        }
    } completion:^(BOOL finished) {
        for (UIView *view in cutViewArray) {
            [view removeFromSuperview];
        }
        [cutViewArray removeAllObjects];
        fromVC.view.hidden = NO;
        [transitionContext completeTransition:YES];
    }];
    
}

-(NSMutableArray *)cutView:(UIView *)view intoSlicesOfWidth:(float)width andHeight:(float)height{
    NSMutableArray *lineViews = [NSMutableArray array];
    UIView *subsnapshot;
    for (int x=0; x<CGRectGetWidth(view.frame); x+=width) {
        for (int y=0;y<CGRectGetHeight(view.frame); y+=height) {
            CGRect subrect = CGRectMake(x, y, width, height);
            subsnapshot = nil;
            subsnapshot = [view resizableSnapshotViewFromRect:subrect afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
            subsnapshot.frame = subrect;
            [lineViews addObject:subsnapshot];
        }
    }
    return lineViews;
}

之后在另外一個代理方法中調(diào)用該方法

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    self.transitionContext = transitionContext;
    UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    [self AnimationAnimationType:transitionContext andToVC:toVC andFromVC:fromVC];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
    [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
    [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;
}

調(diào)用方法劲赠,import 該類

實現(xiàn)以下方法即可

//返回一個管理動畫過渡的對象
-(nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    if (!self.animationRect) {
        self.animationRect = [MJAnimationRect new];
    }
    return self.animationRect;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    if(!self.animationRect){
        self.animationRect = [[MJAnimationRect alloc] init];
    }
    return self.animationRect;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末涛目,一起剝皮案震驚了整個濱河市秸谢,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌霹肝,老刑警劉巖估蹄,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異沫换,居然都是意外死亡臭蚁,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門讯赏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來垮兑,“玉大人,你說我怎么就攤上這事漱挎∠登梗” “怎么了?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵磕谅,是天一觀的道長私爷。 經(jīng)常有香客問我,道長膊夹,這世上最難降的妖魔是什么衬浑? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮放刨,結(jié)果婚禮上工秩,老公的妹妹穿的比我還像新娘。我一直安慰自己宏榕,他們只是感情好拓诸,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著麻昼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪馋辈。 梳的紋絲不亂的頭發(fā)上抚芦,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機與錄音迈螟,去河邊找鬼叉抡。 笑死,一個胖子當著我的面吹牛答毫,可吹牛的內(nèi)容都是我干的褥民。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼洗搂,長吁一口氣:“原來是場噩夢啊……” “哼消返!你這毒婦竟也來了载弄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤撵颊,失蹤者是張志新(化名)和其女友劉穎宇攻,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體倡勇,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡逞刷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了妻熊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片夸浅。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖扔役,靈堂內(nèi)的尸體忽然破棺而出帆喇,到底是詐尸還是另有隱情,我是刑警寧澤厅目,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布番枚,位于F島的核電站,受9級特大地震影響损敷,放射性物質(zhì)發(fā)生泄漏葫笼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一拗馒、第九天 我趴在偏房一處隱蔽的房頂上張望路星。 院中可真熱鬧,春花似錦诱桂、人聲如沸洋丐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽友绝。三九已至,卻和暖如春肝劲,著一層夾襖步出監(jiān)牢的瞬間迁客,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工辞槐, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留掷漱,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓榄檬,卻偏偏與公主長得像卜范,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子鹿榜,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,167評論 25 707
  • 一海雪、定時任務(wù) 方法1:performSelector 方法2:GCD 方法3:NSTimer NSTimer「定時...
    _涼風_閱讀 1,292評論 2 8
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理锦爵,服務(wù)發(fā)現(xiàn),斷路器喳魏,智...
    卡卡羅2017閱讀 134,661評論 18 139
  • A good leader knows when you are frustrated, and then try...
    WW2021閱讀 233評論 0 0
  • 我從2016年10月棉浸,開始完全使用Ubuntu工作,那個時候其實很重要的一個原因是不想玩lol之類的很火的游戲刺彩。畢...
    ltoddy閱讀 350評論 0 2