简单动画

来源:互联网 发布:顶点软件股票牛叉诊股 编辑:程序博客网 时间:2024/05/17 01:13

首先创建storyboard

#import "ViewController.h"


@interface ViewController ()

@property (retain,nonatomic) IBOutletUIImageView *showImage;


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}


//组合动画

- (IBAction)group:(id)sender {

    

    CAAnimationGroup * group = [CAAnimationGroupanimation];

    group.duration =3.0f;

    group.repeatCount =INFINITY;

    

    //放大缩小(transform.scale里的属性)

    CABasicAnimation * scale = [CABasicAnimation        animationWithKeyPath:@"transform.scale"];

//把图片大小转换成动画播放的是时常,所以得转换成NSNumber,fromValuetoValue;

    scale.fromValue = [NSNumbernumberWithFloat:1.0];

    scale.toValue = [NSNumbernumberWithFloat:2.0];

    

    //设置透明度(opacity里的属性)

    CABasicAnimation * basic = [CABasicAnimationanimationWithKeyPath:@"opacity"];

    

    //应为透明度是从10,但是动画播放的是时常,所以得转换成NSNumber,fromValuetoValue;

    basic.fromValue = [NSNumbernumberWithFloat:1];

    basic.toValue = [NSNumbernumberWithFloat:0];

    

    //将两个动画放到group

   NSArray * arr = [NSArrayarrayWithObjects:scale,basic, nil];

    group.animations = arr;

    [self.train.layeraddAnimation:group forKey:@"group"];

  //[self.train.layer removeAnimationForKey:@"group"];  //移除动画

    

}



//透明度

- (IBAction)opacaty:(id)sender {

    

    CABasicAnimation * basic = [CABasicAnimationanimationWithKeyPath:@"opacity"];

    basic.fromValue = [NSNumbernumberWithFloat:1];

    basic.toValue = [NSNumbernumberWithFloat:0];

    basic.duration =1;

    basic.autoreverses =YES;

    basic.repeatCount =INFINITY;

    [self.train.layeraddAnimation:basic forKey:@"scale"];

}


//layer动画能还原

- (IBAction)layerFrame:(id)sender {

    

    CABasicAnimation * basic = [CABasicAnimationanimationWithKeyPath:@"position"];

    basic.toValue = [NSValuevalueWithCGPoint:CGPointMake(0,0)];

    basic.duration =2.0f;

    [self.train.layeraddAnimation:basic forKey:@"donghua"];

    

    

    CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];

    animation.duration =3.0f;

    animation.fromValue = [NSNumbernumberWithFloat:1.0];

    animation.toValue = [NSNumbernumberWithFloat:2.0];

    animation.autoreverses =YES;

    animation.repeatCount =INFINITY;

    [self.train.layeraddAnimation:animation forKey:@"放大"];

    [self.train.layerremoveAnimationForKey:@"放大"];

}

    

- (IBAction)layerScale:(id)sender {

    

    CABasicAnimation * ani = [CABasicAnimationanimationWithKeyPath:@"transform.scale.x"];

    ani.fromValue = [NSNumbernumberWithFloat:1.0];

    ani.toValue = [NSNumbernumberWithFloat:2.0];

    ani.duration =2.0;

    ani.autoreverses =YES;

    [self.train.layeraddAnimation:ani forKey:@"scale"];

}




- (IBAction)frameAction:(id)sender {

    //动画一

    

//    [UIView beginAnimations:@"frame" context:nil];

//    [UIView setAnimationDuration:2];//控制动画速度

//    [self.showImage setFrame:CGRectMake(0, 0, 150, 50)];//frame大小

//    [self.showImage setAlpha:0.2];

//    [UIView commitAnimations];//提交动画

    

    

    //动画二

    

    [UIViewanimateWithDuration:2animations:^{

        [self.showImagesetCenter:CGPointMake(20,180)];//改变动画坐标

        

    }];

    

}


//放大

- (IBAction)scale:(id)sender {

    

   // [UIView animateWithDuration:2 animations:^{

//        self.showImage.transform = CGAffineTransformScale(self.showImage.transform, 1.5, 1.5);//图片持续放大

        

 //   self.showImage.transform = CGAffineTransformMakeScale(2, 2);//放大

    

    //delay延迟时间

      [UIViewanimateWithDuration:3delay:2options:UIViewAnimationOptionCurveLinearanimations:^{

          [self.showImagesetFrame:CGRectMake(60,20,100, 90)];

      }completion:^(BOOL finished) {

          

      }];

}


//翻转视图

- (IBAction)fanzhuan:(id)sender {

    

   UIImageView * aImage = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 202, 188)];

    [aImagesetImage:[UIImageimageNamed:@"2.png"]];

    

    

    [UIViewtransitionWithView:self.trainduration:2options:UIViewAnimationOptionTransitionFlipFromLeftanimations:^{

        

        [self.showImageremoveFromSuperview];  //showImage从俯视图赏移除


        [self.trainaddSubview:aImage];

        

    } completion:NULL];

    

}



0 0
原创粉丝点击