标签:completed bounds app roc oda mis uitouch tom obj
在非常多场景中。我们都须要实现各种动画。这回我们来尝试搞一下控制器间跳转的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
#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;
}
@interface ZYAnimatedTransitioning : NSObject<UIViewControllerAnimatedTransitioning>
@property(nonatomic,assign)BOOL presented;
@end
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
@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的自己定义动画效果了。
标签:completed bounds app roc oda mis uitouch tom obj
原文地址:http://www.cnblogs.com/yfceshi/p/7276786.html