第一次写博客,希望能坚持下去吧,方向为:ios 安卓 swift.
这篇文章我们利用简单的几行代码实现下雪的效果.
雪花素材图片来自阿里巴巴矢量图标库:http://iconfont.cn/search?q=%E9%9B%AA%E8%8A%B1
代码如下,简单易懂,相信只要有一点ios开发基础的都能看懂.
代码复制如下:
//雪花图像
image = [UIImage imageNamed:@"iconfont-xuehua"];
//启动定时器
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(showTime) userInfo:nil repeats:YES];
-(void)showTime
{
//创建雪花
UIImageView *view = [[UIImageView alloc] initWithImage:image];
//添加
[self.view addSubview:view];
//横坐标控制(10-365)
NSInteger startX = arc4random()%356+10;
//设置雪花开始坐标
view.frame = CGRectMake(startX, 50, 50, 50);
//动画
[UIView animateWithDuration:2 animations:^{
view.frame = CGRectMake(startX, self.view.frame.size.height, 30, 30);
} completion:^(BOOL finished) {
//动画完成移除雪花
[view removeFromSuperview];
}];
}
效果图如下:
原文:http://11052428.blog.51cto.com/11042428/1731832