您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 動畫實戰(三):用 CATransition 打造豐富的切換特效

編輯:iOS 開發綜合

流暢而生動的動畫能極大提升 App 的用戶體驗。iOS 提供了多種動畫方案,本文聚焦於 CATransition,帶你實現翻頁、立方體、水波、翻轉等一系列炫酷的視圖切換效果。

準備兩個待切換的視圖

先創建兩個鋪滿全屏、顏色不同的 UIView,用於演示切換效果:

UIView *magentaView = [[UIView alloc] initWithFrame:self.view.bounds];
magentaView.backgroundColor = [UIColor magentaColor];
[self.view addSubview:magentaView];

UIView *grayView = [[UIView alloc] initWithFrame:self.view.bounds];
grayView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:grayView];

基于 UIView 動畫的翻頁效果

利用 UIView 的動畫提交機制,可以實現翻頁式的切換:

// 提交
-(void)add {
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    [UIView commitAnimations];
}
// 翻頁
-(void)curl {
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    [UIView commitAnimations];
}

基于 CATransition 的豐富特效

CATransition 提供了更多種類的過場動畫,通過設置 type 與 subtype 即可實現不同效果:

// 移入
-(void)move {
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = kCATransitionMoveIn;   // 使用 kCATransitionMoveIn 動畫
    transition.subtype = kCATransitionFromLeft; // 指定動畫方向,從左向右
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
}
// 揭開
-(void)reveal {
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = kCATransitionReveal;
    transition.subtype = kCATransitionFromTop;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
}
// 立方體
-(void)cube {
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = @"cube";
    transition.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
}
// 吸入
-(void)suck {
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = @"suckEffect";
    transition.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
}
// 翻轉
-(void)oglFlip {
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = @"oglFlip";
    transition.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
}
// 水波
-(void)ripple {
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = @"rippleEffect";
    transition.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
}

從上面的代碼可以看到,CATransition 的用起來非常統一:設置時長、選擇動畫類型(如 cube、suckEffect、oglFlip、rippleEffect 等)、指定方向,然後把動畫添加到 layer 並交換子視圖即可。掌握這套模式,你就能輕鬆為 App 加入各種絢爛的過場動畫。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved