码迷,mamicode.com
首页 > 其他好文 > 详细

封装雪花下落效果

时间:2016-08-07 13:39:27      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:

效果图如下:

技术分享

代码如下,新建一个View,使他继承UIView,这里面使用的是LzwSnowView;

#import "LzwSnowView.h"

@interface LzwSnowView ()
{
    UIImage *_image;
}
@end

@implementation LzwSnowView


-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        //雪花图片
        _image = [UIImage imageNamed:@"Snow"];
        //循环添加图片
        [NSTimer scheduledTimerWithTimeInterval:.8f target:self selector:@selector(addOneSnowflake) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)addOneSnowflake{
    
    //雪花图片
    UIImageView *imgV = [[UIImageView alloc]initWithImage:_image];
    
    //随机生成雪花位置
    int startX = round(random() % 320);
    int endX = round(random() % 320);
    
    //随机生成雪花的缩放比和速度
    double scale = 1 / round(random() % 100) + 1.0;
    double speed = 1 / round(random() % 100) + 1.0;

    //设置雪花的起始位置
    imgV.frame = CGRectMake(startX, -30, 25.f * scale, 25.f * scale);
    imgV.alpha = .5f;
    
    //将雪花添加到view上面
    [self addSubview:imgV];
    [self sendSubviewToBack:imgV];
    
    //动画改变位置
    [UIView animateWithDuration:25 * speed animations:^{
        //曲线
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        imgV.frame = CGRectMake(endX, 1000.f, 25.f * scale, 25.f * scale);
        
        
    } completion:^(BOOL finished) {
        if (finished) {
            [imgV removeFromSuperview];
        }
        
    }];
    
}


@end

在需要添加雪花的控制器中添加LzwSnowView即可,如下:

LzwSnowView *snowV = [[LzwSnowView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:snowV];
    [self.view sendSubviewToBack:snowV];

OK...

 

封装雪花下落效果

标签:

原文地址:http://www.cnblogs.com/LzwBlog/p/5745953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!