效果:
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;
}