整理一下 Core Animation 常用的轉場類型,並示範 CALayer 動畫與 UIView 動畫兩種寫法。
這些是 CATransition 內建的 type 常數:
kCATransitionFade 淡出
kCATransitionMoveIn 覆蓋原圖
kCATransitionPush 推出
kCATransitionReveal 由底部顯現
以下這些是字串形式的動畫名稱,屬於未公開 API:
pageCurl 向上翻頁
pageUnCurl 向下翻頁
rippleEffect 滴水效果
suckEffect 收縮效果,像一塊布被抽走
cube 立方體翻轉
oglFlip 上下翻轉
使用這類名稱有被 App Store 審核退回的風險,正式產品請避免,僅適合個人實驗或內部 Demo。
#pragma mark UIView 動畫
- (IBAction)pressClick1:(id)sender {
UIButton *button = (UIButton *)sender;
NSInteger tag = button.tag;
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kDuration];
switch (tag) {
case 105:
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
break;
case 106:
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
break;
case 107:
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
break;
case 108:
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
break;
default:
break;
}
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
[UIView setAnimationDelegate:self];
// 動畫結束後可呼叫某方法
// [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
[UIView commitAnimations];
}
#pragma mark layer 動畫
- (IBAction)pressClick2:(id)sender {
UIButton *button = (UIButton *)sender;
NSInteger tag = button.tag;
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = kDuration;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
switch (tag) {
case 101:
animation.type = kCATransitionFade;
break;
case 102:
animation.type = kCATransitionPush;
break;
case 103:
animation.type = kCATransitionReveal;
break;
case 104:
animation.type = kCATransitionMoveIn;
break;
case 201:
animation.type = @"cube";
break;
case 202:
animation.type = @"suckEffect";
break;
case 203:
animation.type = @"oglFlip";
break;
case 204:
animation.type = @"rippleEffect";
break;
case 205:
animation.type = @"pageCurl";
break;
case 206:
animation.type = @"pageUnCurl";
break;
case 207:
animation.type = @"cameraIrisHollowOpen";
break;
case 208:
animation.type = @"cameraIrisHollowClose";
break;
default:
break;
}
switch (self.typeID) {
case 0:
animation.subtype = kCATransitionFromLeft;
break;
case 1:
animation.subtype = kCATransitionFromBottom;
break;
case 2:
animation.subtype = kCATransitionFromRight;
break;
case 3:
animation.subtype = kCATransitionFromTop;
break;
default:
break;
}
self.typeID += 1;
if (self.typeID > 3) {
self.typeID = 0;
}
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
[[self.view layer] addAnimation:animation forKey:@"animation"];
}
UIView 動畫是以 view 為單位,一次包住整個 view 階層的變化,適合畫面級別的切換;CALayer 動畫則作用在 layer 上,粒度更細,也能搭配 sublayerTransform 做 3D 透視效果。
原範例中的 beginAnimations / commitAnimations 這套寫法在 iOS 4 之後已被 block-based 動畫取代。新專案請改用:
[UIView transitionWithView:self.view
duration:kDuration
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
}
completion:nil];
另外提醒,animation.timingFunction 原本誤寫成 UIViewAnimationCurveEaseInOut,這是 UIView 的型別常數,在 CAMediaTimingFunction 上並不適用,已於上方範例中修正為 kCAMediaTimingFunctionEaseInEaseOut。