动画开发

2018-04-15  本文已影响0人  猴跳儿

一、 UIImageView来播放图片组成帧动画

1.创建UIImageView

UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 30, 30)];

//必须设置.frame的值才能正确显示

2.创建对象数组

NSMutableArray * imageArr = [[NSMutableArray alloc]init];

for (int i = 1 ; i <= 4 ; i ++){  //循环设置对象数组的值

       UIImage * image = [UIImage imageNamed:[NSString    stringWithFormat:@"%d.png",i]];

       [imageArr addObject:image];

}

[imageView setAnimationImages:imageArr];  //将对象数组设置为imageView的动画数组

3.设置播放参数

[imageView setAnimationDuration:2];  //播放时长:2秒

[imageView setAnimationRepeatCount:0]; //播放重复次数:0为无限循环

[imageView startAnimating]; //设置开始播放

[imageView stopAnimating]; //设置停止播放

二、UIVIew层的动画

1.创建色块

@interface ViewController (){

UIView * colorView;

}

colorView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];

[colorView setBackgroundColor:[UIColor redColor]];

[colorView.layer setMasksToBounds:YES];

[colorView.layer setCornerRadius:50/2];

[self.view addSubview:colorView];

2.几种创建方法

(一)直接创建

[UIView animateWithDuration:2 animations:^{

[colorView setFrame:CGRectMake(100, 300, 50, 50)];

}];

(二)带完成方法的创建

[UIView animateWithDuration:2 animations:^{

               [colorView setBackgroundColor:[UIColor blueColor]];

}completion:^(BOOL finished) {

                [UIView animateWithDuration:2 animations:^{

                [colorView.layer setCornerRadius:0];

      }];

}];

//可在完成方法中嵌套

(三)带完成方法、延迟和动画的配置参数的创建

[UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{

              [colorView setFrame:CGRectMake(100, 100, 50, 50)];

             [colorView.layer setCornerRadius:50/20];

} completion:nil];

(四)带阻尼系数的创建

[UIView animateWithDuration:2 delay:1 usingSpringWithDamping:1 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{nil} completion:nil];

//usingSpringWithDamping:阻尼系数,取值为0~1,越接近1

上一篇 下一篇

猜你喜欢

热点阅读