http://www.devqinwei.com/2015/10/15/ios實現(xiàn)動畫的幾種常見方式/
1. 使用CALayer的基本關(guān)鍵幀動畫CABasicAnimation和CAKeyframeAnimation
特點:可做3D動畫
詳細(xì)介紹可參看兩個帖子:
http://blog.csdn.net/iosevanhuang/article/details/14488239
http://blog.csdn.net/wscqqlucy/article/details/8669636
注:( CGAffineTransform 和 CATransform3D 的比較 )
CGAffineTransform is used for 2-D manipulation of NSViews, UIViews, and other 2-D Core Graphics elements.
CATransform3D is a Core Animation structure that can do more complex 3-D manipulations of CALayers.( 搬運from stackOverFlow)
如:(CABasicAnimation)
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation.fromValue = @(0);
animation.toValue = @(-M_PI);
animation.repeatCount = 0;
animation.duration = 0.4;
[aView.layer addAnimation:animation forKey:@"rotation"];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / 500.0;
aView.layer.transform = transform;
如:(CAKeyframeAnimation)
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"bounds"];
keyFrameAnimation.delegate = self;
keyFrameAnimation.duration = 1.0f;
keyFrameAnimation.beginTime = CACurrentMediaTime() + 1.0f;
NSValue *initialBounds = [NSValue valueWithCGRect: self.navigationController.view.layer.mask.bounds];
NSValue *secondBounds = [NSValue valueWithCGRect:CGRectMake(0.0f, 0.0f, self.navigationController.view.layer.mask.bounds.size.width - 40.0f , self.navigationController.view.layer.mask.bounds.size.height - 40.0f)];
NSValue *finalBounds = [NSValue valueWithCGRect:CGRectMake(0.0f, 0.0f, 2000.0f, 2000.0f)];
keyFrameAnimation.values = @[initialBounds,secondBounds,finalBounds];
keyFrameAnimation.keyTimes = @[@0, @0.5, @1];
keyFrameAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut] ];
keyFrameAnimation.removedOnCompletion = NO;
keyFrameAnimation.fillMode = kCAFillModeForwards;
[self.navigationController.view.layer.mask addAnimation:keyFrameAnimation forKey:@"maskAnimation"];
2. 使用CALayer的事務(wù)類CATransaction來執(zhí)行
CATransaction動畫包含顯示動畫和隱式動畫兩種帚湘。(顯示能控制動畫中更多方面)
隱式動畫:
//設(shè)置變化動畫過程是否顯示,默認(rèn)為YES不顯示
[CATransaction setDisableActions:NO];
//設(shè)置圓角
self.layer.cornerRadius = (self.layer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
//設(shè)置透明度
self.layer.opacity = (self.layer.opacity == 1.0f) ? 0.5f : 1.0f;
顯示動畫:
//修改執(zhí)行時間
[CATransaction begin];
//顯式事務(wù)默認(rèn)開啟動畫效果,kCFBooleanTrue關(guān)閉
[CATransaction setValue:(id)kCFBooleanFalse
forKey:kCATransactionDisableActions];
//動畫執(zhí)行時間
[CATransaction setValue:[NSNumber numberWithFloat:5.0f] forKey:kCATransactionAnimationDuration];
//[CATransaction setAnimationDuration:[NSNumber numberWithFloat:5.0f]];
self.layer.cornerRadius = (self.layer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
self.layer.opacity = (self.layer.opacity == 1.0f) ? 0.5f : 1.0f;
[CATransaction commit];
另外奸绷,CATransaction來可以用來設(shè)置嵌套動畫,即先執(zhí)行A動畫葱绒,再執(zhí)行B動畫豌蟋,再執(zhí)行C動畫…(這里不做舉例)
(例子來源于:http://www.cnblogs.com/bandy/archive/2012/03/26/2418165.html)
3. 使用UIView關(guān)鍵幀動畫animateKeyframesWithDuration
如:
[UIView animateKeyframesWithDuration:durationTime delay:0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0*frameDuration relativeDuration:1*frameDuration animations:^{
self.pickGradeView.transform = CGAffineTransformMakeScale(0.01, 0.01);
}];
[UIView addKeyframeWithRelativeStartTime:1*frameDuration relativeDuration:1*frameDuration animations:^{
self.pickGradeView.alpha = 1.0;
}];} completion:^(BOOL finished) {
}];
4.使用UIView animateWithDuration
如:
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:0.5 options:0 animations:^{
self.shortNameLabel.transform = CGAffineTransformMakeScale(0.8, 0.8);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
self.shortNameLabel.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:nil];
}];