核心动画
原码:
代码下载请猛戳这里!
核心动画
1.CABasicAnimation:基础动画
- (void)basicAnimation:(CALayer *)layer
{
//1.创建动画对象
CABasicAnimation *animation = [CABasicAnimation animation]
//2.设置动画对象
CGFloat tabbar_height = 49
switch ([self.layers indexOfObject:layer]) {
case 0:
animation.keyPath = @"position"
animation.fromValue = [NSValue valueWithCGPoint:self.view.center]
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(Main_Screen_With/3/2, Main_Screen_Height - Main_Screen_With/3/2 - tabbar_height)]
break
case 1:
animation.keyPath = @"transform.translation.y"
animation.fromValue = [NSNumber numberWithFloat:0]
animation.toValue = [NSNumber numberWithFloat:Main_Screen_Height - Main_Screen_With/3 - tabbar_height]
break
case 2:
animation.keyPath = @"transform.translation"
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(0, Main_Screen_Height - Main_Screen_With/3 - tabbar_height)]
case 3:
animation.keyPath = @"bounds"
animation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, Main_Screen_With/3, Main_Screen_With/3)]
animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, Main_Screen_With/3, -Main_Screen_With/3/5)]
break
case 4:
animation.keyPath = @"transform"
animation.byValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 1, 1, 0)]
break
default:
break
}
animation.duration = 2
animation.delegate = self
animation.removedOnCompletion = NO
animation.fillMode = kCAFillModeForwards
//3.添加动画
[layer addAnimation:animation forKey:nil]
}
2.CAKeyframeAnimation:帧动画
- (void)keyFromAnimationInView:(UIView *)view andTapCount:(NSInteger)count
{
//1.创建动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]
//2.设置动画属性
animation.delegate = self
switch (count) {
case 1:
{
animation.keyPath = @"position"
CGPoint centerP = self.view.center
CGFloat L = [UIScreen mainScreen].bounds.size.width - 100
NSValue *value1 = [NSValue valueWithCGPoint:centerP]
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(centerP.x - L/2, centerP.y - L/2)]
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(centerP.x + L/2, centerP.y - L/2)]
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(centerP.x + L/2, centerP.y + L/2)]
NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(centerP.x - L/2, centerP.y + L/2)]
NSValue *value6 = [NSValue valueWithCGPoint:CGPointMake(centerP.x - L/2, centerP.y - L/2)]
NSValue *value7 = [NSValue valueWithCGPoint:centerP]
animation.values = @[value1,value2,value3,value4,value5,value6,value7]
//animation.keyTimes = @[@(0),@(0.1),@(0.3),@(0.5),@(0.7),@(0.9),@(1)]
animation.duration = 5
animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]
}
break
case 2:
{
animation.keyPath = @"position"
CGMutablePathRef path = CGPathCreateMutable()
CGFloat main_screen_w = [UIScreen mainScreen].bounds.size.width
CGFloat main_screen_h = [UIScreen mainScreen].bounds.size.height
CGFloat view_w = 100
CGPathAddEllipseInRect(path, NULL, CGRectMake(view_w/2, view_w/2, main_screen_w - view_w, main_screen_h - view_w - 49))
animation.path = path
CGPathRelease(path)
animation.duration = 10
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
}
break
case 3:
{
animation.keyPath = @"transform.rotation.z"
animation.values = @[@(0),@(M_PI_4),@(-M_PI_4),@(0)]
animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]
animation.duration = 2
animation.repeatCount = MAXFLOAT
}
break
default:
break
}
animation.removedOnCompletion = NO
animation.fillMode = kCAFillModeForwards
//3.添加动画到图层
[view.layer addAnimation:animation forKey:nil]
}
3.CATransition:过度动画
- (IBAction)swipeGestureAction:(UISwipeGestureRecognizer *)sender {
NSString *picName;
CATransition *animation = [CATransition animation];
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
self.pageNumber --;
if (self.pageNumber < 0) {
self.pageNumber = 8;
}
picName = [NSString stringWithFormat:@"%i.jpg",abs(self.pageNumber + 1)];
animation.type = @"pageUnCurl";
animation.duration = 0.5;
}
else if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
self.pageNumber ++;
if (self.pageNumber > 8) {
self.pageNumber = 0;
}
picName = [NSString stringWithFormat:@"%i.jpg",self.pageNumber + 1];
animation.type = @"pageCurl";
animation.subtype = kCATransitionFromBottom;
animation.duration = 0.5;
}
self.imageView.image = [UIImage imageNamed:picName];
[self.imageView.layer addAnimation:animation forKey:nil];
}
4.CAAnimationGroup:组动画
- (void)groupAnimation
{
//1.创建组动画
CAAnimationGroup *group = [CAAnimationGroup animation]
//2.将其他动画加入组动画中
group.animations = @[[self creatOpacityAnimation],[self creatKeyfromAnimation]]
group.duration = 0.5
group.removedOnCompletion = NO
group.fillMode = kCAFillModeForwards
//1.将动画组添加到图层上
[self.button.layer addAnimation:group forKey:nil]
}
- (CABasicAnimation *)creatOpacityAnimation
{
CABasicAnimation *animation = [CABasicAnimation animation]
animation.keyPath = @"opacity"
animation.fromValue = @(0)
animation.toValue = @(1)
return animation
}
- (CAKeyframeAnimation *)creatKeyfromAnimation
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]
animation.keyPath = @"position"
CGMutablePathRef path = CGPathCreateMutable()
CGPathAddArc(path, NULL, self.view.center.x + 50, self.view.center.y, 50, M_PI, 0, YES)
animation.path = path
return animation
}
5.View动画
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
self.index ++;
if (self.index > 8) {
self.index = 0;
}
self.imageVIew.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",(int)self.index + 1]];
} completion:nil];