当前位置: 代码迷 >> Iphone >> 应用NSTimer与iphone的简单动画,实现飘雪效果
  详细解决方案

应用NSTimer与iphone的简单动画,实现飘雪效果

热度:81   发布时间:2016-04-25 06:33:54.0
使用NSTimer与iphone的简单动画,实现飘雪效果

使用NSTimer与iphone的简单动画,实现飘雪效果,这理原理比较简单,就是定时生成一定的雪花图片,然后使用动画的方式向下漂落(我在其它论坛,看到使用path的方式实现的一个云漂来漂去的效果,实际也可以用那种方式实现,这实际就是前面说的动画效果的两种应用)。所以,我们可以在 viewDidLoad事件中,增加一个图片及定时器并启动,这里的pic请在头文件中定义。

123456
-(void)viewDidLoad{ [super viewDidLoad]; self.pic = [UIImage imageNamed:@"snow.png"];//初始化图片 //启动定时器,实现飘雪效果 [NSTimer scheduledTimerWithTimeInterval:(0.2) target:self selector:@selector(ontime) userInfo:nil repeats:YES];}

然后再实现定时器定时调用的ontime方法:

123456789101112131415
-(void)ontime{ UIImageView *view = [[UIImageView alloc] initWithImage:pic];//声明一个UIImageView对象,用来添加图片 view.alpha = 0.5;//设置该view的alpha为0.5,半透明的 int x = round(random()%320);//随机得到该图片的x坐标 int y = round(random()%320);//这个是该图片移动的最后坐标x轴的 int s = round(random()%15)+10;//这个是定义雪花图片的大小 int sp = 1/round(random()%100)+1;//这个是速度 view.frame = CGRectMake(x, -50, s, s);//雪花开始的大小和位置 [self.view addSubview:view];//添加该view [UIView beginAnimations:nil context:view];//开始动画 [UIView setAnimationDuration:10*sp];//设定速度 view.frame = CGRectMake(y, 500, s, s);//设定该雪花最后的消失坐标 [UIView setAnimationDelegate:self]; [UIView commitAnimations];}

?

  相关解决方案