美文网首页iOS Developer
iOS-简单的图片转场动画

iOS-简单的图片转场动画

作者: linbj | 来源:发表于2017-06-08 16:44 被阅读228次
2017-06-08 16_42_23.gif

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

2017-06-08 16_53_08.gif
实现ViewController根据TransitionFromFirstToSec跳转到SecViewController

ViewController

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

@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<UIViewControllerAnimatedTransitioning>)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<UITouch *> *)touches withEvent:(UIEvent *)event {
    SecViewController *vc = [[SecViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


TransitionFromFirstToSec(实现转场动画的类)

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface TransitionFromFirstToSec : NSObject
<
 UIViewControllerAnimatedTransitioning
>

@end



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

@implementation TransitionFromFirstToSec
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)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<UIViewControllerContextTransitioning>)transitionContext {
    return 0.3;
}
@end


#import <UIKit/UIKit.h>

@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-简单的图片转场动画

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

  • 动画

    CABasicAnimation基础核心动画 缩放动画 图片抖动 根据圆形的路径做移动的效果. 转场动画 创建转场...

  • ios 转场动画

    简单的一句话,转场动画 //转场动画 [UIViewtransitionWithView:self.imageVi...

  • 可以很炫酷的转场动画😏

    今天的目的很简单,做一个仿酷我音乐播放器的转场动画。并以这个案列全面掌握转场动画。先看效果图。如下面的gif图片,...

  • Core Animation之转场动画

    1、转场动画简单介绍 CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。iO...

  • Android 动画-共享元素动画

    一、简介 首先看下转场共享元素动画的效果: 转场共享动画的代码实现: 代码很简单,startActivity 传了...

  • 图片展示、隐藏转场动画

    通过UIViewControllerAnimatedTransitioning实现图片的转场动画,效果类似手机自带...

  • 聊聊Android的转场动画

    Android的转场动画包括:场景动画,页面的转场动画,页面元素间共享动画。 Android转场动画[https:...

  • iOS-自定义控制器转场动画(present/dismiss)

    一、转场动画类型 iOS控制器转场动画类型可以分为非交互式转场动画和交互式转场动画。 二、转场动画分析 2.1、转...

  • iOS - 转场动画

    参考文章:iOS 转场动画一张图看懂 iOS 转场动画iOS自定义转场动画 iOS 转场动画探究(一)

网友评论

    本文标题:iOS-简单的图片转场动画

    本文链接:https://www.haomeiwen.com/subject/agltqxtx.html