IOSdrawRect实现雪花飘落效果
时间:2021-06-16 08:23:20|栏目:iOS代码|点击: 次
本文实例为大家分享了IOSdrawRect实现雪花飘落效果的具体代码,供大家参考,具体内容如下
绘制原理:
雪花效果最主要的思路就是在于循环产生带雪花图片的imageView, 产生的雪花的imageview的 x、y、宽、下落的速度都是随机的,这个可以用随机数来产生数据。
实现代码:
#import <UIKit/UIKit.h> @interface HHFSnowflakeFallingView : UIView /** * 快速创建一个雪花飘落效果的view * * @param bgImageName 背景图片的名称 * @param snowImageName 雪花图片的名称 * @param frame frame * * @return 实例化的 雪花飘落效果的view */ @property(nonatomic,strong) UIImageView *bgImageView; @property(nonatomic,copy) NSString *snowImgName; + (instancetype) snowfladeFallingViewWithBackgroundImageName:(NSString *) bgImageName snowImageName:(NSString *)snowImageName initWithFrame:(CGRect)frame; + //开始下雪 - (void) beginShow; @end
#import "HHFSnowflakeFallingView.h" @implementation HHFSnowflakeFallingView /** * <#Description#> * * @param bgImageName bgImageName 背景图片 * @param snowImageName snowImageName 雪花图片 * @param frame frame 视图的位置和大小 * * @return view 需要绘制的视图 */ + (instancetype) snowfladeFallingViewWithBackgroundImageName:(NSString *) bgImageName snowImageName:(NSString *)snowImageName initWithFrame:(CGRect)frame{ HHFSnowflakeFallingView *view = [[HHFSnowflakeFallingView alloc] initWithFrame:frame]; view.bgImageView.image = [UIImage imageNamed:bgImageName]; view.snowImgName = snowImageName; return view; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.clipsToBounds = YES; //添加背景图片的imageview self.bgImageView = [[UIImageView alloc] init]; self.bgImageView.frame = self.bounds; self.bgImageView.contentMode = UIViewContentModeScaleAspectFill; [self addSubview:self.bgImageView]; } return self; } //开始下雪 - (void) beginShow{ //启动定时器,使得一直调用setNeedsDisplay从而调用- (void) drawRect:(CGRect )rect //不得手动调用- (void) drawRect:(CGRect )rect 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 = arc4random() % 20; while (width < 5) { width = arc4random() % 20; } //雪花的速度 int speed = arc4random() % 15; while (speed < 5) { speed = arc4random() % 15; } //雪花起点y 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); [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 "ViewController.h" #import "HHFSnowflakeFallingView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /** 雪花效果最主要的思路就是在于循环产生带雪花图片的imageView, 产生的雪花的imageview的 x、y、宽、下落的速度都是随机的,这个可以用随机数来产生数据。 */ self.navigationItem.title = @"雪花飘落效果"; //创建雪花飘落效果的view HHFSnowflakeFallingView *snowflakeFallingView = [HHFSnowflakeFallingView snowfladeFallingViewWithBackgroundImageName:@"snow_background" snowImageName:@"snow" initWithFrame:self.view.bounds]; //开始下雪 [snowflakeFallingView beginShow]; [self.view addSubview:snowflakeFallingView]; } @end
运行效果:
上一篇:iOS消息远程推送通知
栏 目:iOS代码
下一篇:iOS指纹识别的简单应用
本文标题:IOSdrawRect实现雪花飘落效果
本文地址:http://www.codeinn.net/misctech/143013.html