iOS学习阶段总结-b20120920-多媒体动画

来源:互联网 发布:linux deploy 安装 编辑:程序博客网 时间:2024/05/29 08:28

UIImageView内置的动画功能

UIImageView.animationImages:保存UIImage的一个数组,实用NSArray.arrayWithObjects创建;

UIImageView.animationDuration:所有图片播放一次的总时间;

UIImageView.animationRepeatCount:重复播放次数,值为0时表示持续播放;

UIImageView.startAnimating:开始播放;

随机动画效果(小项目Snow):

随机数:random(),生成[MIN,MAX]之间的一个随机数 (random()%(MAX-MIN)) + MIN

定时函数:NSTimer.scheduledTimerWithTimeInterval;

关键代码

[SnowViewController.m]viewDidLoad

//设置屏幕背景颜色,浅蓝色

self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1 alpha:1];

//开启定时器,每隔0.05秒执行一次onTimer函数

[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

定时函数onTimer

- (void)onTimer {    UIImage *snowImage = [UIImage imageNamed:@"snow.png"];    UIImageView *view = [[UIImageView alloc] initWithImage:snowImage];    [snowImage release];    //随机开始位置,结束位置,大小,速度    int startX = random()%320;    int endX = random()%320;    int width = random()%MAX_SIZE;//#define定义    int time = (random()%(MAX_TIME - MIN_TIME)) + MIN_TIME;//同上        //开始位置    view.frame = CGRectMake(startX,-1*MAX_SIZE,width,width);//从屏幕上沿出现    view.alpha = 0.25;//半透明    [self.view addSubview:view];    [UIView beginAnimations:nil context:nil];    //速度    [UIView setAnimationDuration:time];    //结束位置    view.frame = CGRectMake(endX,480,width,width);    [UIView commitAnimations];}

程序运行一段时间后,会出现内存溢出

解决内存溢出:注册动画的结束事件,在动画停止时,回收UIImageView

在上面onTimer函数中commitAnimations之前加入下面代码

[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];

[UIView setAnimationDelegate:self];

事件处理:

- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {    UIImageView *snowView = context;    [snowView removeFromSuperview];    [snowView release];}