iOS轉場動畫初探,ios轉場初探

來源:互聯網
上載者:User

iOS轉場動畫初探,ios轉場初探

一般我們就用兩種轉場push和present

present

/** 1.設定代理 - (instancetype)init { self = [super init]; if (self) { self.transitioningDelegate = self; self.modalPresentationStyle = UIModalPresentationCustom; } return self; } 2.添加協議<UIViewControllerTransitioningDelegate> 3.重寫協議方法 - 在協議方法裡面初始化 - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{  }  - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{  } **/

2.push

第一個控制器推第二個控制器的時候

   ViewController2 *vc = [[ViewController2 alloc]init];    self.navigationController.delegate = vc; //必須寫這句話    [self.navigationController pushViewController:vc animated:YES];

第二個控制器

@interface ViewController2 : UIViewController<UINavigationControllerDelegate>@end

代理方法

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {}

上面就是push和present發生的地方,都需要返回一個 UIViewControllerAnimatedTransitioning

所以我們寫一個類

@interface WJTransiation_Mobile : NSObject<UIViewControllerAnimatedTransitioning>

@end

然後修改裡面的協議方法

//動畫時間

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;

//自訂過渡動畫

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

執行過渡動畫

1.擷取前後控制器

   UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

2.獲得轉場動畫視圖

UIView *containerView = [transitionContext containerView];

3.將前後控制器需要進行動畫的視圖添加到containerView上

   [containerView addSubview:tempView];    [containerView addSubview:toVC.view];

4.執行自己的動畫

demo連結:http://pan.baidu.com/s/1kTPPUMb

補:

//動態添加transiationView屬性@interface UIViewController (transiationView)@property (nonatomic,strong)UIView *transiationView;@end
#import <objc/runtime.h>@implementation UIViewController (transiationView)static char transiationViewKey;- (void)setTransiationView:(UIView *)transiationView {    return objc_setAssociatedObject(self, &transiationViewKey, transiationView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (UIView *)transiationView {     return objc_getAssociatedObject(self, &transiationViewKey);}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.