在很多场景中,我们都需要实现各种动画,这回我们来尝试搞一下控制器间跳转的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
这样的话,我们外面用起来就非常简单了:
#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的自定义动画效果了。
时间: 2024-11-05 03:34:25