思路:
控制器A 控制器B 中各有一個(gè)圓形按鈕壕吹,點(diǎn)擊A中白色按鈕會(huì)Push到B控制器
自定義動(dòng)畫部分 創(chuàng)建動(dòng)畫類繼承NSObject遵循UIViewControllerAnimatedTransitioning協(xié)議著蛙,實(shí)現(xiàn)以下方法
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext;
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext;?
計(jì)算小圓和大圓的圓心和半徑耳贬,用CABasicAnimation 繪制路徑動(dòng)添加畫即可
CABasicAnimation *anim = [CABasicAnimation animation];
anim.fromValue = (id)smallPath.CGPath;
anim.toValue = bigPath;
A B控制器都要遵循UINavigationControllerDelegate 代理和實(shí)現(xiàn)以下方法? -(id)navigationController:(UINavigationController*)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC{
//事例
A B控制器都要遵循UINavigationControllerDelegate 代理和實(shí)現(xiàn)以下方法
- (id)navigationController:(UINavigationController*)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC{
? ? if (operation == UINavigationControllerOperationPush) {
? ? ? ? TMTransition *trans = [TMTransition new];
? ? ? ? trans.isPush=YES;
? ? ? ? returntrans;
? ? }else{
? ? ? ? returnnil;
? ? }
}
//在此方法中實(shí)現(xiàn)具體的轉(zhuǎn)場(chǎng)動(dòng)畫
- (void)animateTransition:(id)transitionContext{
? ? //1.持有上下文
? ? _context= transitionContext;
? ? //2.獲取一個(gè)view的容器
? ? UIView*containerView = [transitionContextcontainerView];
? ? //3.獲取tovc的view踏堡,然后添加到容器里面
? ? UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
? ? //4.添加動(dòng)畫
? ? UIButton*btn;
? ? ViewController *VC1;
? ? TMBlueController *VC2;
? ? if(_isPush) {
? ? ? ? VC1 = [transitionContextviewControllerForKey:UITransitionContextFromViewControllerKey];
? ? ? ? VC2 = [transitionContextviewControllerForKey:UITransitionContextToViewControllerKey];
? ? ? ? btn = VC1.mFirstBtn;
? ? }else{
? ? ? ? VC2 = [transitionContextviewControllerForKey:UITransitionContextFromViewControllerKey];
? ? ? ? VC1 = [transitionContextviewControllerForKey:UITransitionContextToViewControllerKey];
? ? ? ? btn = VC2.mBlueBtn;
? ? }
? ? [containerViewaddSubview:VC1.view];
? ? [containerViewaddSubview:VC2.view];
? ? //5.畫一個(gè)小圓(大小圓的中心點(diǎn)是一樣)
? ? UIBezierPath *smallPath = [UIBezierPath bezierPathWithOvalInRect:btn.frame];
? ? //6.中心點(diǎn)
? ? CGPointcenterP;
? ? centerP = btn.center;
? ? //7.求大圓的半徑
? ? CGFloatradius;
? ? //8.判斷按鈕在哪個(gè)象限
? ? CGFloat y = CGRectGetHeight(toVC.view.frame)-btn.center.y;
? ? CGFloat x = CGRectGetWidth(toVC.view.frame) - btn.center.x;
? ? if (btn.frame.origin.x > CGRectGetWidth(toVC.view.frame)/2) {
? ? ? ? if (btn.frame.origin.y < CGRectGetHeight(toVC.view.frame)/2) {
? ? ? ? ? ? //1
? ? ? ? ? ? radius =sqrtf(btn.center.x*btn.center.x+ y*y);
? ? ? ? }else{
? ? ? ? ? ? //4
? ? ? ? ? ? radius =sqrtf(btn.center.x*btn.center.x+ btn.center.y*btn.center.y);
? ? ? ? }
? ? }else{
? ? ? ? if (CGRectGetMaxY(btn.frame) < CGRectGetHeight(toVC.view.frame)/2) {
? ? ? ? ? ? //2
? ? ? ? ? ? radius =sqrtf(x*x + y*y);
? ? ? ? }else{
? ? ? ? ? ? //3
? ? ? ? ? ? radius =sqrtf(btn.center.y*btn.center.y+ x*x);
? ? ? ? }
? ? }
? ? //9.用貝塞爾畫大圓
? ? UIBezierPath *bigPath = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];
? ? //10.把path添加到layer
? ? CAShapeLayer *shapeLayer = [CAShapeLayer layer];
? ? if(_isPush) {
? ? ? ? shapeLayer.path= bigPath.CGPath;
? ? }else{
? ? ? ? shapeLayer.path= smallPath.CGPath;
? ? }
? ? //11.蒙板
? ? UIViewController *VC;
? ? if(_isPush) {
? ? ? ? VC = [transitionContextviewControllerForKey:UITransitionContextToViewControllerKey];
? ? }else{
? ? ? ? VC = [transitionContextviewControllerForKey:UITransitionContextFromViewControllerKey];
? ? }
? ? VC.view.layer.mask= shapeLayer;
? ? //12.動(dòng)畫
? ? CABasicAnimation *anim = [CABasicAnimation animation];
? ? anim.keyPath=@"path";
? ? if(_isPush) {
? ? ? ? anim.fromValue= (id)smallPath.CGPath;
? ? }else{
? ? ? ? anim.fromValue= (id)bigPath.CGPath;
? ? }
? ? //? ? anim.toValue = bigPath;
? ? anim.delegate=self;
? ? [shapeLayeraddAnimation:animforKey:nil];
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag{
? ? [_context completeTransition:YES];
? ? UIViewController *VC;
? ? if(_isPush) {
? ? ? ? VC = [_context viewControllerForKey:UITransitionContextToViewControllerKey];
? ? }else{
? ? ? ? VC = [_context viewControllerForKey:UITransitionContextFromViewControllerKey];
? ? }
? ? VC.view.layer.mask = nil;
}
完整demo: