10、简单动画

2017-01-20  本文已影响0人  深爱久随i

一、UIView基础动画

UIKit 直接将动画继承到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持。执行动画的工作由UIView类自动完成,但仍希望在执行动画时通知视图,为此需要将改变属性的代码放在[UIVIew beginAnimations:nil context:nil]与[UIView commitAnimations]之间。

1、UIView动画

//UIView动画(UIKit动画)
-(void)viewAnimation{
  //早期的动画块  在begin和commit之间写需要改变的视图
    [UIView beginAnimations:@"test1" context:nil];
    //设置动画时长
    [UIView setAnimationDuration:1.0];
    //设置动画是否反转  动画执行完毕,自动原路返回原状态
    [UIView setAnimationRepeatAutoreverses:YES];
    //设置动画重复次数
    [UIView setAnimationRepeatCount:5];
    //在两者之间实现具体代码
    self.testView.backgroundColor=[UIColor yellowColor];
    //改变大小
    CGRect rect=self.testView.bounds;
    rect.size=CGSizeMake(120, 120);
    self.testView.bounds=rect;
    
    //改变位置
    CGRect frame=self.testView.frame;
    frame.origin=CGPointMake(CGRectGetWidth(self.view.frame)-120, CGRectGetHeight(self.view.frame)-120);
    self.testView.frame=frame;
    //提交动画
    [UIView commitAnimations];
    
}

2、UIView层block动画

//UIView层block动画
-(void)blockViewAnimation{
    //第一个参数:动画的时长
   // 第二个参数:将要添加动画的代码在block中实现,相当于将动画的begin和commit之间的代码写在block中
   // 先将动画的设置配置完整,在改变属性值
    [UIView animateWithDuration:1.0 animations:^{
        //设置动画是否反转  动画执行完毕,自动原路返回原状态
        [UIView setAnimationRepeatAutoreverses:YES];
        //设置动画重复次数
        [UIView setAnimationRepeatCount:5];
        //要改变的属性
        self.testView.backgroundColor=[UIColor redColor];
    
        //改变大小
        CGRect rect=self.testView.bounds;
        rect.size=CGSizeMake(120, 120);
        self.testView.bounds=rect;
        
        //改变位置
        CGRect frame=self.testView.frame;
        frame.origin=CGPointMake(CGRectGetWidth(self.view.frame)-120, CGRectGetHeight(self.view.frame)-120);
        self.testView.frame=frame;
    }];
    
    
    //completion:动画执行结束会执行的block
    [UIView animateWithDuration:1.0 animations:^{
        [UIView setAnimationRepeatAutoreverses:YES];
        //设置动画重复次数
       [UIView setAnimationRepeatCount:5];
        //要改变的属性
       self.testView.backgroundColor=[UIColor redColor];
    
    } completion:^(BOOL finished) {
        //当动画执行完成,我们不需要该界面,可以在此处将动画作用的界面隐藏或移除掉
        [self.testView removeFromSuperview];
        NSLog(@"动画执行完毕");
    }];
    
    
    //delay:延时执行  多少秒之后在执行该动画
    //options:动画一些效果的设置,例如是否重复等
    [UIView animateWithDuration:1.0 delay:3.0 options:(UIViewAnimationOptionRepeat) animations:^{
        self.testView.backgroundColor=[UIColor orangeColor];
    } completion:^(BOOL finished) {
        NSLog(@"执行完毕");
    }];
    
    
    //过渡动画   当视图进行切换或者视图位置发生改变的时候,可以考虑使用该动画
//    [UIView transitionWithView:self.testView duration:1.5 options:(UIViewAnimationOptionTransitionFlipFromLeft) animations:^{
//        
//         self.testView.backgroundColor=[UIColor orangeColor];
//    } completion:^(BOOL finished) {
//         NSLog(@"执行完毕");
//    }];
   
}

3、关键帧动画

//关键帧动画
-(void)keyFrameAnimation{
    
    [UIView animateKeyframesWithDuration:4.0 delay:0.0 options:(UIViewKeyframeAnimationOptionRepeat) animations:^{
       //添加动画所需要的帧数
        //第一个参数:当前帧相对的开始时间,取值范围为0~1  例如:如果相对开始时间为0.5,那么他的总时长为0.5*4=2s
        //第二个参数:当前帧相对的时长,取值范围为0~1,例如:此处给值0.5,那么此帧的播放时长为0.5*4=2s
        //第三个参数为:block,在block中实现动画所要做的操作
        //第一帧
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
            
            CGRect rect=self.testView.frame;
            rect.origin.x=CGRectGetWidth(self.view.frame)-rect.size.width;
            self.testView.frame=rect;
    
      }];
 
    //第二帧
    [UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
         CGRect rect=self.testView.frame;
        rect.origin.y=CGRectGetHeight(self.view.frame)-rect.size.height;
        self.testView.frame=rect;

    }];
 
    //第三帧
    [UIView addKeyframeWithRelativeStartTime:0.50 relativeDuration:0.25 animations:^{
        CGRect rect=self.testView.frame;
        rect.origin.x=0;
        self.testView.frame=rect;
    }];
    
    //第四帧
    [UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
          CGRect rect=self.testView.frame;
        rect.origin.y=0;
        self.testView.frame=rect;
    }];
        
    } completion:^(BOOL finished) {
        NSLog(@"执行完毕");
    }];
    
}

4、spring动画

//spring动画
-(void)springAnimation{
    //usingSpringWithDamping:阻尼,取值范围为0-1,值越小,动画效果越明显,抖动的次数多
    //initialSpringVelocity:初始速率,值越大,动画效果越明显,弹得距离大
    //options:动画效果
    [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.1 initialSpringVelocity:20.0 options:(UIViewAnimationOptionCurveLinear) animations:^{
        [UIView setAnimationRepeatAutoreverses:YES];
        self.testView.center=self.view.center;
        
    } completion:^(BOOL finished) {
        self.testView.center=CGPointMake(50, 50);
    }];
    
    
}

二、CoreAnimation动画
1、CoreAnimation动画位于iOS框架的Media层需要添加QuartzCore.Framework(系统自动添加)CoreAnimation基本上是Layer Animation

//layer层的动画
//basicAnimation
-(void)basicAnimation{
    //keyPath:想要改变layer的属性或者是layer的属性的属性
    //keyPath如果书写错误,动画没有效果
    CABasicAnimation* basicAni=[CABasicAnimation animationWithKeyPath:@"position.x"];
    //如果我们只设置toValue一个属性,那就是默认从当前位置到toValue的位置
//    basicAni.toValue=@(CGRectGetWidth(self.view.frame)-50);
    //如果我们只设置byValue属性,那么就是默认从当前位置加byValue的值
    basicAni.byValue=@50;
    //让动画结束之后,不返回原位置
    //当动画结束的时候,是否移除动画所产生的效果
    basicAni.removedOnCompletion=NO;
    //保持哪种状态
    //kCAFillModeForwards:保持动画结束的状态
    basicAni.fillMode=kCAFillModeForwards;
    //fromValue:必须和toValue或者byValue两者中的一个结合使用
    basicAni.fromValue=@100;
    //设置动画时长
    [basicAni setDuration:2.0];
    //将动画添加到layer层
    //forKey:动画标记,移除动画时使用
    [self.testView.layer addAnimation:basicAni forKey:@"basicAni"];
    
}
//layer层的keyframeAnimation
-(void)layerKeyframeAnimation{
    
    //初始化
    CAKeyframeAnimation* keyFrameAni=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    //得到视图原始的position
    CGPoint originPoint=self.testView.layer.position;
    NSValue* originValue=[NSValue valueWithCGPoint:originPoint];
    //一帧
    CGPoint firstPoint=CGPointMake(50, 50);
    NSValue* firstValue=[NSValue valueWithCGPoint:firstPoint];
    //二帧
    CGPoint secondPoint=CGPointMake(CGRectGetWidth(self.view.frame)-50, 50);
    NSValue* secondValue=[NSValue valueWithCGPoint:secondPoint];
    
    //将帧添加到layer上
    keyFrameAni.values=@[originValue,firstValue,secondValue];
    //将动画添加到layer上
    [self.testView.layer addAnimation:keyFrameAni forKey:@"keyFrameAni"];
    keyFrameAni.duration=10.0;
    keyFrameAni.removedOnCompletion=NO;
    keyFrameAni.fillMode=kCAFillModeForwards;
    
    
}


上一篇下一篇

猜你喜欢

热点阅读