iOS 模态动画3

效果:


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

主要用到了物理引擎。

新建动画类MJAinimationRect
在头文件中声明下面两个属性,一个是动画上下文,一个是物理引擎

#import 
#import 
@interface MJAnimationRect : NSObject
@property (strong , nonatomic) id < UIViewControllerContextTransitioning > transitionContext;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end

在实现文件中完成两个代理相对应该实现的代理方法

动画过度时间,直接返回时间值

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

之后准备实现动画效果,代码如下
1.通过上下文获取跳转的view
2.将fromVC截图
3.调用cutView方法将截图切成4*5点格子并添加到当前的containerView上
4.添加之前随机对切图进行微角度旋转,以增加动画效果
5.获取目标view并添加到界面,置于页面底部,暂时先把他的alpha置为0
6.添加作用力
7.设置碰撞检测

-(void)AnimationAnimationType:(id)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

之后在另外一个代理方法中调用该方法

-(void)animateTransition:(id)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;
}

调用方法,import 该类

实现以下方法即可

//返回一个管理动画过渡的对象
-(nullable id )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    if (!self.animationRect) {
        self.animationRect = [MJAnimationRect new];
    }
    return self.animationRect;
}

- (nullable id )animationControllerForDismissedController:(UIViewController *)dismissed{
    if(!self.animationRect){
        self.animationRect = [[MJAnimationRect alloc] init];
    }
    return self.animationRect;
}

你可能感兴趣的:(iOS 模态动画3)