动画总结

最近在学动画,发现好多动画的知识在实际应用中用不到,经常忘记。

动画有:UIView、CALayer、CATransition、CAKeyframeAnimation、CAAnimationGroup几种(非官方,自己总结的)。

代码如下(从书上抄来的)

#import "ViewController.h"

@interface ViewController ()
{
    CALayer *_imageLayer;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self CAPropertyAnimation];
}
- (void)CAPropertyAnimation
{
    _imageLayer = [CALayer layer];
    _imageLayer.cornerRadius = 6;
    _imageLayer.borderWidth = 1;
    _imageLayer.borderColor = [UIColor blueColor].CGColor;
    _imageLayer.masksToBounds = YES;
    _imageLayer.frame = CGRectMake(30, 30, 100, 135);
    _imageLayer.contents = (id)[[UIImage imageNamed:@"image"] CGImage];
    [self.view.layer addSublayer:_imageLayer];
    NSArray * btnTitleArray = @[@"位移",@"旋转",@"缩放",@"动画组"];
    NSMutableArray *btnArray = [[NSMutableArray alloc] init];
    CGFloat totalHeght = [UIScreen mainScreen].bounds.size.height;
    for (int i = 0; i < 4; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setTitle:btnTitleArray[i] forState:UIControlStateNormal];
        NSInteger row = i / 4;
        NSInteger col = i % 4;
        btn.frame = CGRectMake( 15 + col *90, totalHeght - (2 - row)*45 - 20, 70, 35);
        [self.view addSubview:btn];
        [btnArray addObject:btn];
    }
    [btnArray[0] addTarget:self action:@selector(PropertyMove:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[1] addTarget:self action:@selector(PropertyRotate:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[2] addTarget:self action:@selector(PropertyScale:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[3] addTarget:self action:@selector(PropertyGroup:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)CATransitionTest
{
    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];
    NSArray * btnTitleArray = @[@"添加",@"翻页",@"移入",@"揭开",@"立方体",@"吸入",@"翻转",@"水波"];
    NSMutableArray *btnArray = [[NSMutableArray alloc] init];
    CGFloat totalHeght = [UIScreen mainScreen].bounds.size.height;
    for (int i = 0; i < 8; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setTitle:btnTitleArray[i] forState:UIControlStateNormal];
        NSInteger row = i / 4;
        NSInteger col = i % 4;
        btn.frame = CGRectMake( 15 + col *90, totalHeght - (2 - row)*45 - 20, 70, 35);
        [self.view addSubview:btn];
        [btnArray addObject:btn];
    }
    [btnArray[0] addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[1] addTarget:self action:@selector(curl:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[2] addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[3] addTarget:self action:@selector(reveal:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[4] addTarget:self action:@selector(cube:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[5] addTarget:self action:@selector(suck:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[6] addTarget:self action:@selector(oglFilp:) forControlEvents:UIControlEventTouchUpInside];
    [btnArray[7] addTarget:self action:@selector(ripple:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)add:(id)sender
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView commitAnimations];
    [UIView animateWithDuration:1.0f animations:^{
        
    } completion:^(BOOL finished) {
        
    }];
    [UIView animateWithDuration:1.0f animations:^{
        
    }];
}
- (void)curl:(id)sender
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];
    [UIView commitAnimations];
}
- (void)move:(id)sender
{
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
- (void)reveal:(id)sender
{
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = kCATransitionReveal;
    transition.subtype = kCATransitionFromTop;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
- (void)cube:(id)sender
{
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = @"cube";
    transition.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
- (void)suck:(id)sender
{
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = @"suckEffect";
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
- (void)oglFilp:(id)sender
{
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = @"oglFilp";
    transition.subtype = kCATransitionFromBottom;
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
- (void)ripple:(id)sender
{
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.type = @"cameralrisHollowOpen";
    [self.view.layer addAnimation:transition forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
- (void)PropertyMove:(id)sender
{
    CGPoint fromPoint = _imageLayer.position;
    CGPoint toPoint = CGPointMake(fromPoint.x + 80, fromPoint.y);
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
    anim.fromValue = [NSValue valueWithCGPoint:fromPoint];
    anim.toValue = [NSValue valueWithCGPoint:toPoint];
    anim.duration = 0.5;
    _imageLayer.position= toPoint;
    anim.removedOnCompletion = YES;
    [_imageLayer addAnimation:anim forKey:nil];
}
- (void)PropertyRotate:(id)sender
{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D fromValue = _imageLayer.transform;
    anim.fromValue = [NSValue valueWithCATransform3D:fromValue];
    //绕x轴旋转180
    CATransform3D toValue = CATransform3DRotate(fromValue, M_PI / 2, 1, 0, 0);
    //CATransform3D toValue = CATransform3DRotate(fromValue, M_PI, 0, 1, 0);//y
   // CATransform3D toValue = CATransform3DRotate(fromValue, M_PI, 0, 0, 1);//z
    anim.toValue = [NSValue valueWithCATransform3D:toValue];
    anim.duration = 0.5;
    _imageLayer.transform = toValue;
    anim.removedOnCompletion = YES;
    [_imageLayer addAnimation:anim forKey:nil];
}
- (void)PropertyScale:(id)sender
{
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    anim.values = [NSArray arrayWithObjects:[NSValue valueWithCATransform3D:_imageLayer.transform], [NSValue valueWithCATransform3D:CATransform3DScale(_imageLayer.transform, 0.2, 0.2, 1)],[NSValue valueWithCATransform3D:CATransform3DScale(_imageLayer.transform, 2, 2, 1)],[NSValue valueWithCATransform3D:_imageLayer.transform],nil];
    anim.duration = 5;
    anim.removedOnCompletion = YES;
    [_imageLayer addAnimation:anim forKey:nil];
}
- (void)PropertyGroup:(id)sender
{
    CGPoint fromPoint = _imageLayer.position;
    CGPoint toPoint = CGPointMake(280, fromPoint.y + 300);
    CABasicAnimation *moveAnim = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnim.fromValue = [NSValue valueWithCGPoint:fromPoint];
    moveAnim.toValue = [NSValue valueWithCGPoint:toPoint];
    moveAnim.removedOnCompletion = YES;
    CABasicAnimation *transformAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D fromeValue = _imageLayer.transform;
    transformAnim.fromValue = [NSValue valueWithCATransform3D:fromeValue];
    CATransform3D scaleValue = CATransform3DScale(fromeValue, 0.5, 0.5, 1);
    CATransform3D rotateValue = CATransform3DRotate(fromeValue, M_PI, 0, 0, 1);
    CATransform3D toValue = CATransform3DConcat(scaleValue, rotateValue);
    transformAnim.toValue = [NSValue valueWithCATransform3D:toValue];
    transformAnim.cumulative = YES;
    transformAnim.duration = 3;
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = @[moveAnim,transformAnim];
    animGroup.duration = 6;
    [_imageLayer addAnimation:animGroup forKey:nil];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值