直接看轉(zhuǎn)場動畫的協(xié)議時有點迷糊晋渺,做了一個簡單的demo記錄下理解過程
一、如何更改動畫方式
站在自己的角度畴栖,最簡單的理解是八千,要想制作一個自定義的轉(zhuǎn)場動畫,來避開原生的push和present的效果恋捆,則要更改系統(tǒng)默認動畫的代理鸠信。
1.如果你使用的是navigationController的pushViewController方法,則要更改navigationController的delegate星立,并在delegate方法:
- (nullableid)navigationController:(UINavigationController*)navigationController?animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController*)fromVC
toViewController:(UIViewController*)toVC;
{
return[customerAnimator new];//返回一個自定義的動畫執(zhí)行者
}
2.如果你使用的是[currentVCpresentViewController:newVCanimated:YEScompletion:nil];來present新頁面的話绰垂,則需要重新設(shè)置newVC的transitioningDelegate這個代理是實現(xiàn)了UIViewControllerTransitioningDelegate協(xié)議的對象并在delegate中實現(xiàn)協(xié)議的以下兩個方法
- (nullableid)animationControllerForDismissedController:(UIViewController*)dismissed;
{
return[customerAnimator new];//返回一個自定義的動畫執(zhí)行者火焰,在customerAnimator才實現(xiàn)真正的動畫效果,此方法是這個頁面dismiss的之后詢問的代理方法占业,問動畫由設(shè)來執(zhí)行纯赎。
}
- (nullableid)animationControllerForPresentedController:(UIViewController*)presented presentingController:(UIViewController*)presenting sourceController:(UIViewController*)source;
{
return[customerAnimator new];//返回一個自定義的動畫執(zhí)行者,此方法在頁面出現(xiàn)時詢問念恍,問出現(xiàn)的動畫由誰來執(zhí)行晚顷。
}
二、動畫的實際效果怎么實現(xiàn)
從上面的協(xié)議方法中可以看出该默,協(xié)議最終都要求返回一個遵循UIViewControllerAnimatedTransitioning協(xié)議的對象,即customerAnimator匣摘,這個對象充當動畫的執(zhí)行者角色。
也就是說必搞,這個執(zhí)行者必須按照協(xié)議規(guī)定的樣子去執(zhí)行轉(zhuǎn)場囊咏,這個animator必須實現(xiàn)的兩個協(xié)議方法是:
//1.執(zhí)行動畫所需要的時間
- (NSTimeInterval)transitionDuration:(nullableid)transitionContext;
//2.轉(zhuǎn)場動畫效果具體實現(xiàn)在這個方法中去實現(xiàn)
- (void)animateTransition:(id)transitionContext;
所以可以看出,整個過程中核心是animator(動畫的執(zhí)行者)在起作用霜第,核心方法是- (void)animateTransition:(id)transitionContext 户辞,負責(zé)實現(xiàn)實際的效果實現(xiàn)
//簡單的示例
- (void)animateTransition:(id)transitionContext {
//如何切換兩個View,設(shè)置和動畫寫在此處
//toVC
UIViewController*toVC = [transitionContextviewControllerForKey:UITransitionContextToViewControllerKey];
//finally rect
CGRectfinallyRect = [transitionContextfinalFrameForViewController:toVC];
toVC.view.frame=CGRectOffset(finallyRect,0, [UIScreenmainScreen].bounds.size.height);
//3
[[transitionContextcontainerView]addSubview:toVC.view];
//4
[UIViewanimateWithDuration:[selftransitionDuration:transitionContext]delay:0.0options:UIViewAnimationOptionCurveLinearanimations:^{
toVC.view.frame= finallyRect;
}completion:^(BOOLfinished) {
[transitionContextcompleteTransition:YES];
}];
}
三刃榨、基于手勢的轉(zhuǎn)場動畫
當在animator中實現(xiàn)了動畫效果后双仍,要想在手指向右滑動的過程中,動畫也跟著手指的滑動程度去更新動畫的執(zhí)行程度苞轿,則需要實現(xiàn)一個代理方法逗物,用于返回標志動畫執(zhí)行的百分比的實例。
同樣契邀,如果是使用UIviewController presentViewController 的方式 則是實現(xiàn)UIViewControllerTransitioningDelegate協(xié)議的方法
- (nullableid)interactionControllerForDismissal:(id)animator{
_persentDriver =[UIPercentDrivenInteractiveTransition new];
return_persentDriver;//此實例中紀錄了動畫執(zhí)行的persent
}
//用于提供消失的時候的手勢百分比
如果是使用navigationController的push方法將viewController推出的莲祸,則需要實現(xiàn)naviagtionControllerDelegate協(xié)議的
- (nullableid)navigationController:(UINavigationController*)navigationController?interactionControllerForAnimationController:(id) animationController {
_persentDriver =[UIPercentDrivenInteractiveTransition new];
return_persentDriver;
}
代理方法
為了達到手在滑動的過程中動畫跟著手指執(zhí)行,需要在給viewontroller的view加一個滑動手勢
UIPanGestureRecognizer*panGesture = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(handPanGesture:)];
[self.viewaddGestureRecognizer:panGesture];
- (void)hanPanGesture:(UIPanGestureRecognizer*)gesture {
? ? ?CGPointpoint = [gesturetranslationInView:_viewController.view];
? ? ?switch(gesture.state) {
? ? ? ? ? ?caseUIGestureRecognizerStateBegan:
? ? ? ? ? ?{
? ? ? ? ? ? ? ? if(point.y>0) {
? ? ? ? ? ? ? ? ? ? ? ?if(![self isBePresent])
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //push進來的
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[self.navigationControllerpopViewControllerAnimated:YES];
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//present進來的
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [selfdismissViewControllerAnimated:YEScompletion:^{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? }
} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?case ?UIGestureRecognizerStateChanged: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?CGFloatpersent = point.y/200.0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[_persentDriverupdateInteractiveTransition:persent];//更新動畫的百分比 ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?case ?UIGestureRecognizerStateCancelled: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?case ?UIGestureRecognizerStateEnded: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [_persentDriverfinishInteractiveTransition]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?default:
?break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } }
- (BOOL)isBePresented {
? ? ? ? if(self.navigationController) {
? ? ? ? ? ? ? ?NSArray*viewcontrollers=self.navigationController.viewControllers;
? ? ? ? ? ? ? if(viewcontrollers.count>1) {
? ? ? ? ? ? ? ? ? ? ? ?if([viewcontrollersobjectAtIndex:viewcontrollers.count-1]==self) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //push方式
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? returnNO;
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ?//present方式
? ? ? ? ? ? ? ? returnYES;
? ? ? ? ? ? }
? ? ?}
returnNO;
}
將手勢的執(zhí)行和persentDriver數(shù)據(jù)更新鏈接起來后缴阎,UINaviagtionControllerDelegate和UIViewControllerTransitioningDelegate
協(xié)議中返回的_persentDriver才可以帶有手勢的執(zhí)行比例,這樣動畫就可以跟著手指滑動的程度來更新了述暂。