美文网首页PYKit程序员手机移动程序开发
OC_转场动画的工具类(无交互)

OC_转场动画的工具类(无交互)

作者: LiYaoPeng | 来源:发表于2017-03-22 00:12 被阅读73次

转场动画的学习

请看简书iOS CAAnimation之CATransition(自定义转场动画)


一、思路

. a跳转b

  1. a: a可以什么都不用做,直接present,
  2. b: b要在init方法里面 写这两个方法,
    这个方法保证fromView才不会被移除(及可以在modal后看到a控制器的view)
    self.modalPresentationStyle = UIModalPresentationCustom;
    这个属性表示在modal、dismiss的时候会走自定义的方法
    self.transitioningDelegate = self.animatr;

二、Animatr 方法 && 属性

1. 构造方法
.*这里需要注意,要给定modalPresentationStyle,否则会有坑:请看后面的"坑1"

/**
 * modalPresentationStyle toVC中设置的转场动画的样式
 */
+(instancetype)animatrWithModalPresentationStyle: (UIModalPresentationStyle)modalPresentationStyle;
/**
 * modalPresentationStyle toVC中设置的转场动画的样式
 */
-(instancetype)initWithModalPresentationStyle: (UIModalPresentationStyle)modalPresentationStyle;

. *dismiss & present 动画具体回调方法

//MARK: ---------------------- dismiss & present ------------------------
/**dismiss动画*/
-(void)dismissAnimaWithBlock: (void(^)(UIViewController *toVC, UIViewController *fromeVC, UIView *toView, UIView *fromeView))dismissAnimaBlock;
/**present动画*/
-(void)presentAnimaWithBlock: (void(^)(UIViewController *toVC, UIViewController *fromeVC, UIView *toView, UIView *fromeView))presentAnimaBlock;

. *容器视图的view,可以用作遮罩,修改ContainerView的方法

//MARK: ---------------------- setupContainerView ------------------------
-(void)setupContainerViewWithBlock: (void(^)(UIView *containerView))setupContainerViewBlock;

2. 属性

/**这是属性一定要设置,否则看 上面解释的“坑1”*/
@property (nonatomic,assign) UIModalPresentationStyle modalPresentationStyle;
//MARK:  -------------------- 动画时长 和类型 ------------------------
/** present动画时长*/
@property (nonatomic,assign) CGFloat presentDuration;
/** dismiss动画时长*/
@property (nonatomic,assign) CGFloat dismissDuration;
/**动画是否完成,在动画完成时候,一定要把这个属性改为YES*/
@property (nonatomic,assign) BOOL isAccomplishAnima;

三、具体实现

注意 : 一切都在toVC中设置

  1. 设置属性(类延展中相对私有属性)
@interface PushViewController ()
@property (nonatomic,strong) Animatr *animatr;
@end
  1. 在懒加载中或者viewDidLoad中设置相关属性和实现相关方法
-(void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    [self setupAnimatr];//设置Animatr
}
//设置Animatr
-(void)setupAnimatr {
    //dismiss动画预估时长
    _animatr.dismissDuration = 4;
    //present动画预估时长
    _animatr.presentDuration = 5;
    //dismiss转场动画
    [_animatr dismissAnimaWithBlock:^(UIViewController *toVC, UIViewController *fromeVC, UIView *toView, UIView *fromeView) {
        NSLog(@"dismiss开始");
        [UIView animateWithDuration:_animatr.dismissDuration animations:^{
            fromeView.frame = CGRectMake(0, 0, 100, 100);
        } completion:^(BOOL finished) {
            //在完成动画的时候一定要把这个属性设置成YES 告诉系统动画完成
            _animatr.isAccomplishAnima = YES;
        }];
    }];
    //present转场动画
    [_animatr presentAnimaWithBlock:^(UIViewController *toVC, UIViewController *fromeVC, UIView *toView, UIView *fromeView) {
        [UIView animateWithDuration:_animatr.presentDuration animations:^{
            toView.frame = CGRectMake(0,300, 300, 300);
        } completion:^(BOOL finished) {
            //在完成动画的时候一定要把这个属性设置成YES 告诉系统动画完成
            _animatr.isAccomplishAnima = YES;
        }];
    }];
    //容器视图,装有toView和fromeView,可以作为遮罩
    [_animatr setupContainerViewWithBlock:^(UIView *containerView) {
        containerView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
    }];
}

四、github地址OC版转场动画工具类

相关文章

网友评论

本文标题:OC_转场动画的工具类(无交互)

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