首页 > 代码库 > 自己定义modal动画
自己定义modal动画
在非常多场景中。我们都须要实现各种动画。这回我们来尝试搞一下控制器间跳转的modal动画。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
ZYSecondViewController *second = [[ZYSecondViewController alloc]init];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:second animated:YES completion:nil];
}
上面是系统提供的动画的样式,可是系统提供的动画有时候满足不了我们的需求,所以我们就要自己定义动画了。我们接下来。就重点的来说一下自己定义动画这一块的内容。
准备工作:我之前写过的单例:一行代码搞定单例
以及一些坐标的扩展,这里我们直接调用一下:
ZYtransition.h(单例)
#import "Singleton.h"
@interface ZYtransition : NSObject <UIViewControllerTransitioningDelegate>
SingletonH(transition)
@end
- ZYtransition.m
#import "ZYtransition.h"
#import "ZYAnimatedTransitioning.h"
#import "ZYPresentationController.h"
@implementation ZYtransition
SingletonM(transition)
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
ZYPresentationController *pc = [[ZYPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
return pc;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
ZYAnimatedTransitioning *anim = [[ZYAnimatedTransitioning alloc]init];
anim.presented = YES;
return anim;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
ZYAnimatedTransitioning *anim = [[ZYAnimatedTransitioning alloc]init];
anim.presented = NO;
return anim;
}
- ZYAnimatedTransitioning.h (负责动画)
@interface ZYAnimatedTransitioning : NSObject<UIViewControllerAnimatedTransitioning>
@property(nonatomic,assign)BOOL presented;
@end
- ZYAnimatedTransitioning.m
const NSTimeInterval duraton = 0.5;
@implementation ZYAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
return duraton;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// UITransitionContextToViewKey
// UITransitionContextFromViewKey
if (self.presented)
{
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
toView.y = -toView.height;
[UIView animateWithDuration:duraton animations:^{
toView.y = 0;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}else
{
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
[UIView animateWithDuration:duraton animations:^{
fromView.y = -fromView.height;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
@end
- 搞一个ZYPresentationController
.h
@interface ZYPresentationController : UIPresentationController
.m
@implementation ZYPresentationController
- (void)presentationTransitionWillBegin
{
self.presentedView.frame = self.containerView.bounds;
[self.containerView addSubview:self.presentedView];
}
- (void)presentationTransitionDidEnd:(BOOL)completed
{
// NSLog(@"%s",__func__);
}
- (void)dismissalTransitionWillBegin
{
// NSLog(@"%s",__func__);
}
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
[self.presentedView removeFromSuperview];
}
@end
这种话,我们外面用起来就非常easy了:
#import "ViewController.h"
#import "ZYSecondViewController.h"
#import "ZYtransition.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
ZYSecondViewController *second = [[ZYSecondViewController alloc]init];
second.modalPresentationStyle = UIModalPresentationCustom;
second.transitioningDelegate = [ZYtransition sharedtransition];
[self presentViewController:second animated:YES completion:nil];
}
@end
外面仅仅须要跟平时一样。然后设置显示的样式为自己定义,然后制定代理。就能实现modal的自己定义动画效果了。
自己定义modal动画
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。