iOS-简单的图片转场动画

2017-06-08 16_42_23.gif

需要一个类似这样的效果,发现是用push跳转界面的,于是想到了转场动画。

iOS-简单的图片转场动画_第1张图片
2017-06-08 16_53_08.gif
实现ViewController根据TransitionFromFirstToSec跳转到SecViewController

ViewController

  1. 实现UINavigationControllerDelegate的代理方法
//下面的方法是操作行为是push还是pop
返回UIViewControllerAnimatedTransitioning特定的转场动画
- (id)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC ;
#import 

@interface ViewController : UIViewController

@property (nonatomic, strong) UIImageView *image;

@end


#import "ViewController.h"
#import "SecViewController.h"
#import "TransitionFromFirstToSec.h"

@interface ViewController ()
<
 UINavigationControllerDelegate
>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _image = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, self.view.frame.size.width-200, 300)];
    _image.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_image];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // Set outself as the navigation controller's delegate so we're asked for a transitioning object
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // Stop being the navigation controller's delegate
    if (self.navigationController.delegate == self) {
        self.navigationController.delegate = nil;
    }
}


#pragma mark UINavigationControllerDelegate methods

- (id)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC {
    if (fromVC == self && [toVC isKindOfClass:[SecViewController class]]) {
        return [[TransitionFromFirstToSec alloc] init];
    }
    else {
        return nil;
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    SecViewController *vc = [[SecViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


TransitionFromFirstToSec(实现转场动画的类)

#import 
#import 

@interface TransitionFromFirstToSec : NSObject
<
 UIViewControllerAnimatedTransitioning
>

@end



#import "TransitionFromFirstToSec.h"
#import "ViewController.h"
#import "SecViewController.h"

@implementation TransitionFromFirstToSec
- (void)animateTransition:(id)transitionContext {
    ViewController *fromViewController  = (ViewController*)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    SecViewController *toViewController = (SecViewController*)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *containerView     = [transitionContext containerView];
    NSTimeInterval duration   = [self transitionDuration:transitionContext];

    UIImageView *img          = (UIImageView *)fromViewController.image;
    UIView *cellImageSnapshot = [img snapshotViewAfterScreenUpdates:NO];
    cellImageSnapshot.frame   = [containerView convertRect:img.frame fromView:img.superview];
    img.hidden                = YES;

    // Setup the initial view states
    toViewController.view.frame   = [transitionContext finalFrameForViewController:toViewController];
    toViewController.view.alpha   = 0;
    toViewController.image.hidden = YES;
    
    [containerView addSubview:toViewController.view];
    [containerView addSubview:cellImageSnapshot];
    
    [UIView animateWithDuration:duration animations:^{
        // Fade in the second view controller's view
        toViewController.view.alpha = 1.0;
        // Move the cell snapshot so it's over the second view controller's image view
        CGRect frame = [containerView convertRect:toViewController.image.frame fromView:toViewController.view];
        cellImageSnapshot.frame = frame;
    } completion:^(BOOL finished) {
        // Clean up
        toViewController.image.hidden = NO;
        img.hidden = NO;
        [cellImageSnapshot removeFromSuperview];
        
        // Declare that we've finished
        [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
    }];
}

- (NSTimeInterval)transitionDuration:(id)transitionContext {
    return 0.3;
}
@end


#import 

@interface SecViewController : UIViewController
@property (nonatomic, strong) UIImageView *image;

@end

#import "SecViewController.h"

@interface SecViewController ()

@end

@implementation SecViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    _image.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_image];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

其他动画待完成

你可能感兴趣的:(iOS-简单的图片转场动画)