使用NSTimer与iphone的简单动画,实现飘雪效果,这理原理比较简单,就是定时生成一定的雪花图片,然后使用动画的方式向下漂落,所以,我们可以在 viewDidLoad事件中,增加一个图片及定时器并启动.
-(void)viewDidLoad{ [super viewDidLoad]; self.pic = [UIImage imageNamed:@"snow.png"];//初始化图片 //启动定时器,实现飘雪效果 [NSTimer scheduledTimerWithTimeInterval:(0.2) target:self selector:@selector(ontime) userInfo:nil repeats:YES]; }
然后再实现定时器定时调用的ontime方法:
-(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]; }
本文介绍如何使用NSTimer和UIImageView在iPhone应用中实现简单的飘雪动画效果。通过定时生成雪花图片并利用动画使其向下飘落,营造出冬日氛围。
90

被折叠的 条评论
为什么被折叠?



