iOS_三种简单动画的实现

1:头尾式动画.

// beginAnimations 表示此后的代码要参与到动画中
[UIView beginAnimations:nil context:nil];
// 设置动画持续时间
[UIView setAnimationDuration:0.5];
        
self.testView.frame = CGRectMake(screenW, screenH, 100, 100);

// commitAnimations,将beginAnimation之后的所有动画提交并生成动画
[UIView commitAnimations];

说明:如果只是修改控件的属性,使用首尾式动画还是比较方便的,但是如果需要在动画完成后做后续处理,就不是那么方便了

2:block 动画


    CGFloat screenW = [UIScreen mainScreen].bounds.size.width - 120;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height - 120;

    [UIView animateWithDuration:0.7 animations:^{
        self.testView.frame = CGRectMake(screenW, screenH, 100, 100);
    } completion:^(BOOL finished) {
        self.testView.backgroundColor = [UIColor blueColor];
    }];

说明:
(1)在实际的开发中更常用的时block代码块来处理动画操作。
(2)块动画相对来说比较灵活,尤为重要的是能够将动画相关的代码编写在一起,便于代码的阅读和理解.

3:序列帧动画



    UIImageView *img = [[UIImageView alloc]init];
    
    //这里把要执行动画的图片存放到数组中即可 
    NSMutableArray *arrayM = [NSMutableArray array];
    
    // 给图片框设置图片数组
    [img setAnimationImages:arrayM];
    
    // 设置动画播放时间
    [img setAnimationDuration:arrayM.count * 0.075];
    
    // 设置重复次数(默认是0)
    [img setAnimationRepeatCount:1];
    
    // 开始动画
    [img startAnimating];
    
    // 停止动画
    [img stopAnimating];
    
    // 动画是否在执行
    BOOL isAnim = [img isAnimating];

说明:
项目中使用序列帧动画,要考虑程序性能,(内存暴涨)播放动画以后要清空图片数组.

你可能感兴趣的:(iOS_三种简单动画的实现)