FlakeDown-2

一个生成雪花的定时器,随机大小和位置,下落和消失动画。



//定时器,生成雪花***

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

}

//生成雪花的方法

- (void)flakeDown {

//注意:定时器调用的方法中,创建对象不用全局变量

UIImageView *imageView = [[UIImageView alloc] init];

imageView.image= [UIImage imageNamed:@"flake.png"];

//随机生成一个雪花宽度10 - 40

//获取随机数:先获取随机范围内的数字个数

int width =arc4random() % 31 + 10;

//随机生成雪花的横坐标

int x1 = arc4random() % (321-width);

imageView.frame=CGRectMake(x1, -width, width, width);

[self.view addSubview:imageView];

//让雪花下落

[UIView beginAnimations:nil context:(__bridgevoid*)(imageView)];

//随机一个动画时间,控制下落速度2.0 - 5.0

float time = (arc4random()%31 + 20)/10.0;

NSLog(@"%.1f",time);

[UIView setAnimationDuration:time];

//下落曲线为加速

[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

//动画结束调方法

[UIView setAnimationDelegate:self];

[UIViewset AnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

//随机一个雪花落地时的横坐标

int x2 =arc4random() % (321-width);

imageView.frame=CGRectMake(x2, 480-width, width, width);

[UIView commitAnimations];

}

//动画结束之后调用的方法。使雪花消失

- (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {

UIImageView *flake = (__bridgeUIImageView*)context;

[UIView beginAnimations:nil context:nil];

//[UIView setAnimationDelay:1];

[UIView setAnimationDuration:1];

flake.alpha= 0;

[UIView commitAnimations];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容