所謂的動畫組就是將一些動畫組合起來給layer使其的動畫更豐富靈活。 它很簡單,就是為其animations屬性賦值一個動畫數組。
- (void)demoAnimationGroup
{
static NSString * const kCAPostionKeyPath = @"position";
static NSString * const kCAOpacityKeyPath = @"opacity";
static NSString * const kCARotationKeyPath = @"transform.rotation";
static NSString * const kCAScaleKeyPath = @"transform.scale";
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:_demoView.layer.position];
[path addCurveToPoint:CGPointMake(260, 400) controlPoint1:CGPointMake(0, 460) controlPoint2:CGPointMake(320, 0)];
//路徑動畫
CAKeyframeAnimation *posAnimation = [CAKeyframeAnimation animationWithKeyPath:kCAPostionKeyPath];
posAnimation.path = path.CGPath;
posAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//透明度動畫
CABasicAnimation *opaAnimation = [CABasicAnimation animationWithKeyPath:kCAOpacityKeyPath];
opaAnimation.duration = 2.0f;
opaAnimation.toValue = @(0.3f);
opaAnimation.autoreverses = YES;
//旋轉動畫
CABasicAnimation *rotAnimation = [CABasicAnimation animationWithKeyPath:kCARotationKeyPath];
rotAnimation.duration = 2.0f;
rotAnimation.toValue = @(M_PI);
rotAnimation.autoreverses = YES;
//縮放動畫
CABasicAnimation *scaAnmaiton = [CABasicAnimation animationWithKeyPath:kCAScaleKeyPath];
scaAnmaiton.duration = 2.0f;
scaAnmaiton.toValue = @(1.5f);
scaAnmaiton.autoreverses = YES;
//設置動畫組
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[posAnimation, opaAnimation, rotAnimation, scaAnmaiton];
group.duration = 4.0f;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[_demoView.layer addAnimation:group forKey:nil];
}
這個動畫組對demoView的layer加入了一些同時進行的動畫,比如雙控制點的貝塞爾路徑,透明度的漸隱漸明,旋轉以及縮放等。 CATransition 轉場動畫幾乎在所有的APP上都能遇見,經常用於視圖控制器的切換或者單視圖控制器的頁面切換等,也可以在自定義UIStoryboardSegue中來加入動畫效果。 它也是CAAnimation的子類,能為圖層提供移出屏幕和移入的動畫效果。 其常用屬性為 type 過渡類型 subtype 過渡類型的子類型,方向設置 其中過渡類型有很多,最初有四個版本,後來又加入了一些,以字符串形式設置。 最初的四個:fade,push,moveIn,reveal 以後加入的效果則更加豐富和炫目,有 cube, oglFlip, suckEffect, rippleEffect, pageCurl, pageUnCurl, cameraIrisHollowOpen, cameraIrisHollowClose。
- (void)viewTransition
{
static NSString * const kCATransitionTypeFlip = @"oglFlip"; //翻頁效果
CATransition *transition = [CATransition animation];
transition.type = kCATransitionTypeFlip;
transition.subtype = kCATransitionFromRight;
transition.duration = 1.5f;
[_transitionOrangeView.layer addAnimation:transition forKey:nil];
[self.view performSelector:@selector(sendSubviewToBack:) withObject:_transitionOrangeView afterDelay:1.0f];
}
這個方法簡單的實現了view翻頁的效果,當然目前控制器的根view只有一個subview,所以使用sendSubviewToBack的方法後顯示的還是這個view,但是看到了動畫效果。 現在UIView的轉場動畫有了更方便的類方法 + transitionWithView:duration:options:animations:completion: + transitonFromView:toView:duration:options:completion: 這兩個方法參數加入了蘋果推薦的塊代碼,也讓轉場動畫都在一個方法中進行處理,也對動畫結束進行了處理,更加方便易用。
- (void)anotherTransition
{
_transitionBlueView = [[UIView alloc] initWithFrame:self.view.bounds];
_transitionBlueView.backgroundColor = [UIColor blueColor];
[UIView transitionFromView:_transitionOrangeView
toView:_transitionBlueView
duration:1.0f
options:UIViewAnimationOptionTransitionCrossDissolve
completion:nil];
}
代碼很簡潔和易讀。 不過要注意的一點是,這裡的參數並不是很多,而且我並沒有對藍色視圖通過addSubview加載到self.view中,也沒有對橘色視圖進行removeFromSuperview,這些方法都封裝在這個類方法中隱式進行了。