本文实例为大家分享了iosdrawrect实现雪花飘落效果的具体代码,供大家参考,具体内容如下
绘制原理:
雪花效果最主要的思路就是在于循环产生带雪花图片的imageview, 产生的雪花的imageview的 x、y、宽、下落的速度都是随机的,这个可以用随机数来产生数据。
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#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
|
运行效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/light_of_life/article/details/52185778