UI效果iOS软件技术

iOS开发-定制属于你自己的酷炫转场动画

2016-06-28  本文已影响2146人  CharlesAir
i fought,but i lost,then i rest

MyZone


先看效果图(模拟器上interaction-dismiss,因为无法精准的控制Pinch力度,导致效果不佳,真机上运行很流畅)

效果图.gif

你可以从这篇文章中学到:


如果你想实现更多的转场效果,可以深入研究下facebook 的 pop开源框架,大有裨益


项目目录结构如下-转场相关:

转场相关的类.png

限于篇幅的缘故,这里着重介绍下如何自定义一个modal的转场

创建一个自定义的modal转场

实现自定义的转场,一般遵循三个步骤

1.首先创建一个专门负责动画的控制器 animation controller,此控制器遵循UIViewControllerAnimatedTransitioning protocol.该控制器负责转场动画的实现.

该例中具体代码如下:

#import <UIKit/UIKit.h>

@interface BoncePresentAnimationController : NSObject<UIViewControllerAnimatedTransitioning>

@end

实现协议中两个非常重要的方法


//1.该方法指定动画时长
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext

//2.该方法指定转场动画的具体实现
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext

结合到具体实例代码如下

#import "BoncePresentAnimationController.h"

@implementation BoncePresentAnimationController



// This method specifies the length of the transition animation.
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    
    return  0.5;
    
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    
    
    /**1.从当前上下文中获得相应的状态*/
    
    UIViewController * toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIViewController * fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    
    
    CGRect finalFrame = [transitionContext finalFrameForViewController:toViewController];
    
    /**2.获得容器视图*/
    UIView * containerView = [transitionContext containerView];
 
    
    /**3.设置初始状态*/
    
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    
    toViewController.view.frame = CGRectOffset(screenBounds, 0, screenBounds.size.height);
    
    
    /**4.添加视图*/
    
    [containerView addSubview:toViewController.view];
    
    /**5.动画*/
    NSTimeInterval  duration = [self transitionDuration:transitionContext];
    
    
    [UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
       
        fromViewController.view.alpha = 0.5;
        
        toViewController.view.frame = finalFrame;
        
    } completion:^(BOOL finished) {
        
        fromViewController.view.alpha = 1;
        
        // Inform the transition context when the animation completes. The framework then ensures the final state is consistent and removes the from- view from the container.
        [transitionContext completeTransition:YES];
        
    }];
    
}

2.在modal出目标控制器之前,指定目标控制器的转场代理,并且遵循UIViewControllerTransitioningDelegate协议

本例中代码如下

if ([segue.identifier isEqualToString:@"showAbout"]) {
        
        UIViewController * toViewController = segue.destinationViewController;
        
        [_pinchInteractionController wireToViewController:toViewController];
        
        toViewController.transitioningDelegate = self;
        
    }
   

3.在代理方法中,返回转场动画控制器


#pragma mark - UIViewControllerTransitioningDelegate(Modal相关)

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    
    //1.this method simply returns your custom animation controller instance. If you had multiple view controllers wired up, you could also check the view controller presented to switch between different custom animations.
    return _bouncePresentAnimationController;
    
}

notice

下图可能更清晰的描述上文所述

流程 2016-06-28 上午12.41.26.png

综上所述,一个简单的自定义modal专场基本实现

另附上项目地址

notice

1.以上代码仅供参考,如果有任何你觉得不对的地方,都可以联系我,我会第一时间回复,谢谢.
qq:391565521 email:zhuhaifei_ios@163.com

持续完善中,敬请期待.......

上一篇下一篇

猜你喜欢

热点阅读