美文网首页
iOS CADisplayLink实现雪花飘落的效果

iOS CADisplayLink实现雪花飘落的效果

作者: 打不死的小怪兽 | 来源:发表于2016-12-27 13:42 被阅读73次
    屏幕快照 2016-12-27 下午1.33.12.png
    #import <UIKit/UIKit.h>
    
    @interface CADisplayView : UIView
    @property (nonatomic, strong) UIImageView *bgImageViiew;
    @property (nonatomic, copy) NSString *snowImgName;
    +(instancetype)snowFladeFallingViewWithBackGroundImgName:(NSString *)bgImageName snowImageName:(NSString *)snowImageName initWithFrame:(CGRect)frame;
    - (void)beginSnow;
    
    @end
    
    #import "CADisplayView.h"
    #import "HJCornerRadius.h"
    @implementation CADisplayView
    
    +(instancetype)snowFladeFallingViewWithBackGroundImgName:(NSString *)bgImageName snowImageName:(NSString *)snowImageName initWithFrame:(CGRect)frame {
        CADisplayView *view = [[CADisplayView alloc]initWithFrame:frame];
        view.bgImageViiew.image = [UIImage imageNamed:bgImageName];
        view.snowImgName = snowImageName;
        return view;
    }
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            self.clipsToBounds = YES;
            self.bgImageViiew = [[UIImageView alloc] init];
            self.bgImageViiew.frame = self.bounds;
            self.bgImageViiew.contentMode = UIViewContentModeScaleAspectFill;
            [self addSubview:self.bgImageViiew];
        }
        return self;
    }
    - (void)beginSnow {
        CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
        [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    }
    - (void)drawRect:(CGRect)rect {
        if (self.subviews.count>250) {
            return;
        }
        //雪花的宽度
        int width = 8;
        //雪花的速度
        int speed = arc4random()%15;
        while (speed<5) {
            speed = arc4random()%15;
        }
        int startY = - (arc4random() % 100);
        //雪花起点x
        int startX = arc4random() % (int) [UIScreen mainScreen].bounds.size.width;
        //雪花终点x
        int endX = arc4random() % (int) [UIScreen mainScreen].bounds.size.width;
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.snowImgName]];
        imageView.frame = CGRectMake(startX, startY, width, width);
        imageView.aliCornerRadius = width/2;
        [self addSubview:imageView];
        [UIView animateWithDuration:speed animations:^{
            //设置雪花最终的frame
            imageView.frame = CGRectMake(endX, [UIScreen mainScreen].bounds.size.height, width, width);
            //设置雪花的旋转
            imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI);
            imageView.alpha = 0.3;
        } completion:^(BOOL finished) {
            [imageView removeFromSuperview];
        }];
    }
    
    @end
    
    
    #import "CADisplayViewController.h"
    #import "CADisplayView.h"
    
    @interface CADisplayViewController ()
    
    @end
    
    @implementation CADisplayViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading theview.
        self.view.backgroundColor = [UIColor whiteColor];
        CADisplayView *snow = [CADisplayView snowFladeFallingViewWithBackGroundImgName:@"smilegirl" snowImageName:@"ll" initWithFrame:self.view.bounds];
        [snow beginSnow];
        [self.view addSubview:snow];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    

    相关文章

      网友评论

          本文标题:iOS CADisplayLink实现雪花飘落的效果

          本文链接:https://www.haomeiwen.com/subject/qvzsvttx.html