不廢話直接上代碼属提,我們首先新建個(gè)工程,然后再創(chuàng)建一個(gè)控制器彪杉,分別給兩個(gè)控制器設(shè)置兩個(gè)背景色吧
說明:我們的轉(zhuǎn)場(chǎng)自定義動(dòng)畫的代碼是寫在單獨(dú)創(chuàng)建的工具類里面的行贪,先看push的
TransPushTool.h
#import <UIKit/UIKit.h>//首先把這個(gè)頭文件要從#import <Foundation/Foundation.h>改為UIKit的
//這里是要遵守這個(gè)動(dòng)畫轉(zhuǎn)場(chǎng)的協(xié)議
@interface TransPushTool : NSObject<UIViewControllerAnimatedTransitioning>
@end
TransPushTool.m
@implementation TransPushTool
//遵守那個(gè)協(xié)議后,我們有兩個(gè)必須實(shí)現(xiàn)的方法抵代,在這里面來寫你自己想要的動(dòng)畫方式
#返回轉(zhuǎn)場(chǎng)動(dòng)畫執(zhí)行時(shí)間
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 2;
}
#利用轉(zhuǎn)場(chǎng)上下文寫動(dòng)畫
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
/**<#文檔注釋#>***
1.transitionContext:轉(zhuǎn)場(chǎng)上下文
2.fromVc:來自哪個(gè)ViewController
3.toVc:去哪個(gè)控制器
**/
//獲取view(根據(jù)key獲取view)
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
#我們push動(dòng)畫中用不到fromView
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
#動(dòng)畫的容器:所有設(shè)置的view都必須添加到這里面來
UIView *containerView = [transitionContext containerView];
//讓要呈現(xiàn)的view透明度為0
toView.alpha = 0;
[containerView addSubview:toView];
#寫動(dòng)畫(動(dòng)畫時(shí)間和上面的轉(zhuǎn)場(chǎng)的時(shí)間必須一樣)
[UIView animateWithDuration:2.0 animations:^{
toView.alpha = 1;
} completion:^(BOOL finished) {
//轉(zhuǎn)場(chǎng)完成
[transitionContext completeTransition:YES];
}];
}
#至此在這個(gè)工具類中我們做的配置已寫完了
#首先在那個(gè)ViewController.m文件中導(dǎo)入那個(gè)push動(dòng)畫工具類和下一個(gè)viewController頭文件
//我們是自定義push動(dòng)畫腾节,所以要給viewController加上導(dǎo)航控制器哦(再遵守導(dǎo)航控制器的協(xié)議)
@interface ViewController ()<UINavigationControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
//設(shè)置導(dǎo)航控制器的代理
self.navigationController.delegate = self;
}
//轉(zhuǎn)場(chǎng)按鈕(在storyboard中拖一個(gè)按鈕)
- (IBAction)pushBtn {
//正常的push轉(zhuǎn)場(chǎng)就好
DetalViewController *vc = [[DetalViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -- 實(shí)現(xiàn)導(dǎo)航控制器的代理方法
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
//判斷當(dāng)前是push還是pop
if (operation == UINavigationControllerOperationPush)
{
return [[TransPushTool alloc]init];
}else
#如果我們寫了pop的動(dòng)畫工具類的話,這里就填pop的
return nil;
}
#到這里就可以實(shí)現(xiàn)那個(gè)漸變出來的push轉(zhuǎn)場(chǎng)效果了
接下來我們寫個(gè)pop動(dòng)畫的工具類
TransPopTool.h
#import <UIKit/UIKit.h>
//遵守協(xié)議
@interface TransPopTool : NSObject<UIViewControllerAnimatedTransitioning>
TransPopTool.m
#import "TransPopTool.h"
@implementation TransPopTool
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 2;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
//獲取view
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
//給toView的形變屬性設(shè)值(縮小一點(diǎn)點(diǎn))
toView.transform = CGAffineTransformMakeScale(0.9, 0.9);
UIView *containerView = [transitionContext containerView];
//將toView加到FromView之下
[containerView insertSubview:toView belowSubview:fromView];
//動(dòng)畫
[UIView animateWithDuration:2 animations:^{
//把來自的view放到屏幕下面去
fromView.center = CGPointMake(fromView.center.x, [UIScreen mainScreen].bounds.size.height * 2);
//將這個(gè)恢復(fù)其形變屬性
toView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[fromView removeFromSuperview];
[transitionContext completeTransition:YES];
}];
}
#方法和push寫動(dòng)畫是一樣的荤牍,我們?cè)趺磳?shí)現(xiàn)呢
//只需在viewControll中將導(dǎo)航控制器的代理方法那里改為
//判斷當(dāng)前是push還是pop
if (operation == UINavigationControllerOperationPush)
{
return [[TransPushTool alloc]init];
}else
return [[TransPopTool alloc]init];
}
接下來我們看看那個(gè)present 和 dismiss 的轉(zhuǎn)場(chǎng)該怎么寫
首先我們這兩個(gè)工具類是不用動(dòng)的案腺,我們只需在下個(gè)控制器中設(shè)置一下代碼就好(可以把導(dǎo)航的注釋掉吧)
#在要present出來的控制器的.m文件中
#import "DetalViewController.h"
//導(dǎo)入那兩個(gè)工具類的頭文件(名字可以忽略哈)
#import "TransPushTool.h"
#import "TransPopTool.h"
//遵守這樣的一個(gè)協(xié)議
@interface DetalViewController ()<UIViewControllerTransitioningDelegate>
@end
@implementation DetalViewController
- (void)viewDidLoad {
[super viewDidLoad];
//設(shè)置代理
self.transitioningDelegate = self;
}
#點(diǎn)擊屏幕dismiss到上一個(gè)控制器
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- 實(shí)現(xiàn)那個(gè)present和dismiss的代理方法
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
return [[TransPushTool alloc]init];
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
return [[TransPopTool alloc]init];
}
在viewController中我們來個(gè)present就好
//轉(zhuǎn)場(chǎng)按鈕
- (IBAction)pushBtn {
DetalViewController *vc = [[DetalViewController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
}
總結(jié):上面是很簡(jiǎn)單的自定義轉(zhuǎn)場(chǎng)動(dòng)畫的兩種方式,你會(huì)發(fā)現(xiàn)只要把轉(zhuǎn)場(chǎng)的兩個(gè)工具類封裝起來康吵,以后你的push 和 pop 還有present救湖,dismiss 都可以用了。在push 和 pop 的自定義動(dòng)畫工具類可以寫成一個(gè)工具類的涎才,只需判斷一下是push 還是 pop 轉(zhuǎn)場(chǎng)
網(wǎng)上有很多炫酷的轉(zhuǎn)場(chǎng)效果,感興趣可以自己去借鑒學(xué)習(xí),不過任何困難的東西都是從基礎(chǔ)來的耍铜,所以懂得基礎(chǔ)原理還是很重要的
最后編輯于 :2017.12.03 07:06:17
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者