iOS动画

1.UIView属性动画

<1>方法一

- (IBAction)animationAction1:(UIButton *)sender {
    NSLog(@"属性动画");
    // 1.开始动画 第一个参数是名字
    [UIView beginAnimations:@"属性动画" context:nil];
    // 2.设置属性
    // 1)设置动画的持续时间
    [UIView setAnimationDuration:2];
    // 2)动画延迟执行时间
    [UIView setAnimationDelay:1];
    // 3)设置重复次数
    [UIView setAnimationRepeatCount:2];
    // 4)设置自动翻转
    [UIView setAnimationRepeatAutoreverses:YES];
    // 5)设置动画效果控制
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    // 设置代理
    [UIView setAnimationDelegate:self];
    // 6)给动画开始的时候添加事件(一定要先设置代理)
    [UIView setAnimationWillStartSelector:@selector(startAnimation)];
    // 7)给动画结束的时候添加事件
    [UIView setAnimationDidStopSelector:@selector(stopAnimation)];
    // 动画效果
    _redView.center = CGPointMake(300, 80);
    // 3.结束动画
    [UIView commitAnimations];
   }

<2>方法二 block

- (IBAction)animationAction2:(UIButton *)sender {
    NSLog(@"block动画");
    // 属性设置可以写在block块之间
      方法一
    [UIView animateWithDuration:1 animations:^{
        [UIView setAnimationDelay:2];
       // 动画运行时需要的操作
        _redView.backgroundColor = [UIColor yellowColor];
    }];
    方法二
    [UIView animateWithDuration:2 animations:^{
        // 进行时
    } completion:^(BOOL finished) {
        // 完成时
    }];    方法三
    [UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionAutoreverse animations:^{
        [UIView setAnimationRepeatCount:4];
    } completion:^(BOOL finished) {       
    }]; 
}

<3>UIViewTransition 过渡效果

- (IBAction)animationAction3:(UIButton *)sender {
    NSLog(@"视图切换过渡效果");
    // 和UI第三节课容器视图控制器切换子视图控制器的方法类似
    [UIView transitionFromView:_blueView toView:_greenView duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) {
        NSLog(@"切换成功");
    }];
       [UIView transitionWithView:_blueView duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
        _blueView.backgroundColor = [UIColor grayColor];
    } completion:^(BOOL finished) {     
    }];
}

2.CGAffineTransform

- (IBAction)animationAction4:(UIButton *)sender {
    NSLog(@"2D仿射变换");
    [UIView beginAnimations:@"2D仿射变换" context:nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationRepeatCount:3];
#pragma mark 旋转
    // 旋转 基于原始
    _redView.transform = CGAffineTransformMakeRotation(M_PI_2/3);
    // 旋转 基于上一次
    _redView.transform = CGAffineTransformRotate(_redView.transform, M_PI_2/3);
#pragma mark 缩放
    _redView.transform = CGAffineTransformMakeScale(3, 0.5);
    _redView.transform = CGAffineTransformMake(-1, cos(M_PI_2/3), 3, sin(M_PI_4), 1, 1);
    [UIView commitAnimations];
}

3.CABasicAnimation

- (IBAction)animationAction5:(UIButton *)sender {
    NSLog(@"Basic");
    // 参数:需要改变什么就写什么 不能有差异
       //   改变bounds
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
    CGSize fromSize = CGSizeMake(100, 100);
    CGSize toSize = CGSizeMake(20, 200);
    // 动画只是一个效果 不会改变属性的值,若想要改变属性的值,需要我们手动区改变
    CGRect newRect = _redView.layer.bounds;
    newRect.size = toSize;
    _redView.layer.bounds = newRect;
    // 设置初始值
    basic.fromValue = [NSValue valueWithCGSize:fromSize];
    // 设置结束值
    basic.toValue = [NSValue valueWithCGSize:toSize];
       // 改变frame
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds"];
    CGRect fromFrame = CGRectMake(100, 100, 100, 100);
    CGRect toFrame = CGRectMake(20, 20, 20, 20);
    _redView.layer.bounds = toFrame;
    basic.fromValue = [NSValue valueWithCGRect:fromFrame];
    basic.toValue = [NSValue valueWithCGRect:toFrame];
    // 添加动画
    [_redView.layer addAnimation:basic forKey:@"test"];
       // 震动效果
    // 取出redView的layer层 
   CALayer *myLayer = _redView.layer;
    // 获取layer的位置
    CGPoint position = myLayer.position;
    // 设置晃动时两个终点的位置
    CGPoint x1 = CGPointMake(position.x-10, position.y);
    CGPoint x2 = CGPointMake(position.x+10, position.y);
    // 设置动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    // 设置开始位置
    [animation setFromValue:[NSValue valueWithCGPoint:x1]];
    // 设置结束位置
    [animation setToValue:[NSValue valueWithCGPoint:x2]];
    // 设置自动反转
    [animation setAutoreverses:YES];
    // 设置持续时间
    [animation setDuration:0.06];
    // 设置重复次数 
   [animation setRepeatCount:100];
    // 添加
    [myLayer addAnimation:animation forKey:@"test"];
}

4.CAKeyframeAnimation

- (IBAction)animationAction6:(UIButton *)sender {
    NSLog(@"keyFrame");
    // 创建并指定路径
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    // 设置运动轨迹
    keyFrame.values = [NSArray arrayWithObjects:[
       NSValue valueWithCGPoint:CGPointMake(50, 170)],
       [NSValue valueWithCGPoint:CGPointMake(364, 170)],
       [NSValue valueWithCGPoint:CGPointMake(110, 364)],
       [NSValue valueWithCGPoint:CGPointMake(207, 50)],
       [NSValue valueWithCGPoint:CGPointMake(300, 364)],
       [NSValue valueWithCGPoint:CGPointMake(50, 170)
], nil];
    // 设置持续时间
    keyFrame.duration = 6;
    // 设置关键帧的时间 比分时间
    keyFrame.keyTimes = [NSArray arrayWithObjects:[
       NSNumber numberWithFloat:0.1],
       [NSNumber numberWithFloat:0.2],
       [NSNumber numberWithFloat:0.3],
       [NSNumber numberWithFloat:0.4],
       [NSNumber numberWithFloat:0.5],
       [NSNumber numberWithFloat:0.6], nil];
    // 添加动画
    [_redView.layer addAnimation:keyFrame forKey:@"test1"];
}

5.CAAnimationGroup

- (IBAction)animationAction7:(UIButton *)sender {
    NSLog(@"group");
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
    CGSize fromSize = CGSizeMake(100, 100);
    CGSize toSize = CGSizeMake(20, 200);
    // 动画只是一个效果 不会改变属性的值,若想要改变属性的值,需要我们手动区改变
    CGRect newRect = _redView.layer.bounds;
    newRect.size = toSize;
    _redView.layer.bounds = newRect;
    // 设置初始值
    basic.fromValue = [NSValue valueWithCGSize:fromSize];
    // 设置结束值
    basic.toValue = [NSValue valueWithCGSize:toSize];
       // 创建并指定路径
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    // 设置运动轨迹
    keyFrame.values = [NSArray arrayWithObjects:[
        NSValue valueWithCGPoint:CGPointMake(50, 170)],
        [NSValue valueWithCGPoint:CGPointMake(364, 170)],
        [NSValue valueWithCGPoint:CGPointMake(110, 364)],
        [NSValue valueWithCGPoint:CGPointMake(207, 50)],
        [NSValue valueWithCGPoint:CGPointMake(300, 364)],
        [NSValue valueWithCGPoint:CGPointMake(50, 170)], nil];
    // 设置持续时间
    keyFrame.duration = 6;
    // 设置关键帧的时间 比分时间
    keyFrame.keyTimes = [NSArray arrayWithObjects:[
        NSNumber numberWithFloat:0.1],
        [NSNumber numberWithFloat:0.2],
        [NSNumber numberWithFloat:0.3],
        [NSNumber numberWithFloat:0.4],
        [NSNumber numberWithFloat:0.5],
        [NSNumber numberWithFloat:0.6], nil];
    // 创建group动画
    CAAnimationGroup *group = [CAAnimationGroup animation];
    // 设置持续时间
    group.duration = 8;
    // 将动画添加到数组中
    group.animations = @[keyFrame,basic];
       [_redView.layer addAnimation:group forKey:@"test"];
}

6.CATransition

- (IBAction)animationAction8:(UIButton *)sender {
    NSLog(@"transition");
    // 创建layer过渡动画
    CATransition *sition = [CATransition animation];
    // 设置持续时间
    sition.duration = 2;
    // 设置过渡方向
    sition.subtype = kCATransitionFromBottom;
    // 设置过渡效果
    // 1.系统提供的样式 
   sition.type = kCATransitionPush;
    // 2.私有API
    sition.type = @"suckEffect";
    // 添加动画
    [_redView.layer addAnimation:sition forKey:@"test"];
}

你可能感兴趣的:(iOS动画)