有次被问到QQ音乐的那个动画怎么做的,网上搜了下,发现没找到iOS 的,不过搜到一个swift 的,就自己参照改了下。希望对你们有帮助。
swift 下载地址:https://github.com/BourneWeng/MagicMove,
iOS 代码:
1.viewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak,nonatomic)UITableViewCell * seletedCell;/**< 动画图片的父视图*/
@end
viewController.m
#import "ViewController.h"
#import "RootViewController.h"
#import "MyTranstions.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView=[UITableView new];
tableView.delegate=self;
tableView.dataSource=self;
tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableViewCell"];
}
cell .imageView.image = [UIImage imageNamed:@"imageView"];
cell .textLabel.text=@"嘿嘿饿";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.seletedCell = [tableView cellForRowAtIndexPath:indexPath];
self.seletedCell.selected =false;
RootViewController *ctr = [RootViewController new];
ctr.transitioningDelegate = [MyTranstions shareTransitioning];
[self presentViewController:ctr animated:true completion:nil];
}
2.RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@property (strong,nonatomic)UIImageView *imageView;/**< 动画图片最终显示*/
@end
RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
CGFloat width =self.view.bounds.size.width ;
self.view.backgroundColor = [UIColor whiteColor];
self.imageView = [UIImageView new];
self.imageView.frame = CGRectMake((width-136)/2, 60, 136, 171);
self.imageView.image = [UIImage imageNamed:@"imageView"];
[self.view addSubview:self.imageView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self dismissViewControllerAnimated:false completion:nil];
}
3.MyTranstions.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyTranstions : NSObject <UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>{
}
+(instancetype)shareTransitioning;
@end
MyTranstions.m
#import "MyTranstions.h"
#import "ViewController.h"
#import "RootViewController.h"
static MyTranstions *_instance;
typedef NS_ENUM(NSUInteger, Status){
Presented=0,
Dismissed
};
@interface MyTranstions()
@property (nonatomic,assign)Status currentStatus;
@end
@implementation MyTranstions
+(instancetype)shareTransitioning
{
if (_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [MyTranstions new];
});
}
return _instance;
}
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
self.currentStatus = Presented;
return self;
}
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
self.currentStatus = Dismissed;
return self;
}
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.6f;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIView *containerView = [transitionContext containerView];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
if (self.currentStatus == Presented) {
ViewController * fromMenuVC = (ViewController *)fromVC;
RootViewController *toMenuVC= (RootViewController *)toVC;
UIView *snapshotView =[fromMenuVC.seletedCell.imageView snapshotViewAfterScreenUpdates:false];
snapshotView .frame = [containerView convertRect:fromMenuVC.seletedCell.imageView.frame fromView:fromMenuVC.seletedCell];
toMenuVC.imageView.hidden =true;
toMenuVC.view.frame = [transitionContext finalFrameForViewController:toMenuVC];
toMenuVC.view.alpha = 0 ;
[containerView addSubview:toMenuVC.view];
[containerView addSubview:snapshotView];
[toMenuVC.imageView layoutIfNeeded];
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
snapshotView.frame = toMenuVC.imageView.frame;
toMenuVC.view.alpha = 1;
} completion:^(BOOL finished) {
toMenuVC.imageView.hidden = false ;
toMenuVC.imageView.image = fromMenuVC.seletedCell.imageView.image;
[snapshotView removeFromSuperview];
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}else{
ViewController * toMenuVC = (ViewController *)toVC;
RootViewController *fromMenuVC= (RootViewController *)fromVC;
UIView * snapshotView = [fromMenuVC.imageView snapshotViewAfterScreenUpdates:false];
snapshotView.frame = [containerView convertRect:fromMenuVC.imageView.frame fromView:fromVC.view];
fromMenuVC.imageView.hidden = true;
toMenuVC.view.frame = [transitionContext finalFrameForViewController:toMenuVC];
toMenuVC.seletedCell.imageView.hidden = true ;
[containerView insertSubview:toMenuVC.view belowSubview:fromMenuVC.view];
[containerView addSubview:snapshotView];
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
snapshotView.frame = [containerView convertRect:toMenuVC.seletedCell.imageView.frame fromView:toMenuVC.seletedCell];
fromMenuVC.view.alpha = 0 ;
} completion:^(BOOL finished) {
toMenuVC.seletedCell.imageView.hidden = false ;
[snapshotView removeFromSuperview];
fromMenuVC.imageView.hidden = false ;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
}
@end
原文:http://www.cnblogs.com/heiheihei/p/5175929.html