iOS开发之UI基础--三种简单的动画设置

来源:互联网 发布:淘宝包邮规则 编辑:程序博客网 时间:2024/04/29 08:04

一、首尾式动画

示例代码:


// beginAnimations表示此后的代码要“参与到”动画中    [UIView beginAnimations:nil context:nil];//设置动画时长    [UIView setAnimationDuration:2.0];          self.headImageView.bounds = rect;    // commitAnimations,将beginAnimation之后的所有动画提交并生成动画    [UIView commitAnimations];

二.block动画
示例代码:

//简单的动画效果    [UIView animateWithDuration:2.0 animations:^{        image1.alpha=0;    } completion:^(BOOL finished) {        [image1 removeFromSuperview];    }];
//1.<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在实际的开发中更常用的时block代码块来处理动画操作。</span>
<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">//2.块动画相对来说比较灵活,尤为重要的是能够将动画相关的代码编写在一起,便于代码的阅读和理解.</span>


三.序列帧动画

1.示例代码:

NSMutableArray  *arrayM=[NSMutableArray array];    for (int i=0; i<40; i++) {        [arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"eat_%02d.jpg",i]]];    }    //设置动画数组    [self.tom setAnimationImages:arrayM];    //设置动画播放次数    [self.tom setAnimationRepeatCount:1];    //设置动画播放时间    [self.tom setAnimationDuration:40*0.075];    //开始动画    [self.tom startAnimating];

2. UIImageView的序列帧动画(需要考虑程序性能,释放数据)

// 0. 是否正在动画

[self.tom isAnimating];

// 1. 设置图片的数组

[self.tom setAnimationImages:arrayM];

// 2. 设置动画时长,默认每秒播放30张图片

[self.tom setAnimationDuration:arrayM.count * 0.075];

// 3. 设置动画重复次数,默认为0,无限循环

[self.tom setAnimationRepeatCount:1];

// 4. 开始动画

[self.tom startAnimating];

// 5. 动画播放完成后,清空动画数组

[self.tom performSelector:@selector(setAnimationImages:) withObject:nilafterDelay:self.tom.animationDuration];






0 0