iOS SnowAnimation(下雪动画)~demo

//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄

/**

注意点: 1.看 GIF 效果图.

2.看连线视图的效果图.

3.看实现代码(直接复制实现效果).

*/

一、GIF 效果图:

二、连线视图的效果图:

图1:

图2:

图3:

三、实现代码:

=========================

===================================================

==========================

控制器1:SHContext.h

//

//  SHContext.h

//  SnowAnimation(下雪动画)~demo

//

//  Created by石虎on 2017/8/15.

//  Copyright © 2017年shihu. All rights reserved.

//

#ifndef SHContext_h

#define SHContext_h

/*

该方法负责绘制圆角矩形

x1、y2:是圆角矩形左上角的座标。

width、height:控制圆角举行的宽、高

radius:控制圆角矩形的四个圆角的半径

*/

voidCGContextAddRoundRect(CGContextRefc,CGFloatx1 ,CGFloaty1

,CGFloatwidth ,CGFloatheight ,CGFloatradius)

{

//移动到左上角

CGContextMoveToPoint(c, x1 + radius , y1);

//添加一条连接到右上角的线段

CGContextAddLineToPoint(c , x1 + width - radius, y1);

//添加一段圆弧

CGContextAddArcToPoint(c , x1 + width , y1, x1 + width

, y1 + radius, radius);

//添加一条连接到右下角的线段

CGContextAddLineToPoint(c , x1 + width, y1 + height - radius);

//添加一段圆弧

CGContextAddArcToPoint(c , x1 + width, y1 + height

, x1 + width - radius , y1 + height , radius);

//添加一条连接到左下角的线段

CGContextAddLineToPoint(c , x1 + radius, y1 + height);

//添加一段圆弧

CGContextAddArcToPoint(c , x1, y1 + height , x1

, y1 + height - radius , radius);

//添加一条连接到左上角的线段

CGContextAddLineToPoint(c , x1 , y1 + radius);

//添加一段圆弧

CGContextAddArcToPoint(c , x1 , y1 , x1 + radius , y1 , radius);

}

/*

该方法负责绘制多角星。

n:该参数通常应设为奇数,控制绘制N角星。

dx、dy:控制N角星的中心。

size:控制N角星的大小

*/

voidCGContextAddStar(CGContextRefc ,NSIntegern

,CGFloatdx ,CGFloatdy ,NSIntegersize)

{

CGFloatdig =4*M_PI/ n ;

//移动到指定点

CGContextMoveToPoint(c , dx , dy + size);

for(inti =1; i <= n ; i++)

{

CGFloatx =sin(i * dig);

CGFloaty =cos(i * dig);

//绘制从当前点连接到指定点的线条

CGContextAddLineToPoint(c , x * size + dx ,y * size + dy);

}

}

/*

该方法负责绘制花朵。

n:该参数控制花朵的花瓣数。

dx、dy:控制花朵的位置。

size:控制花朵的大小

length:控制花瓣的长度

*/

voidCGContextAddFlower(CGContextRefc ,NSIntegern

,CGFloatdx ,CGFloatdy ,CGFloatsize ,CGFloatlength)

{

//移动到指定点

CGContextMoveToPoint(c , dx , dy + size);

CGFloatdig =2*M_PI/ n;

//采用循环添加n段二次曲线路径

for(inti =1; i < n +1; i++)

{

//结算控制点坐标

CGFloatctrlX =sin((i -0.5) * dig) * length + dx;

CGFloatctrlY=cos((i -0.5) * dig) * length + dy;

//结算结束点的坐标

CGFloatx =sin(i * dig) * size + dx;

CGFloaty =cos(i * dig) * size + dy;

//添加二次曲线路径

CGContextAddQuadCurveToPoint(c, ctrlX , ctrlY , x , y);

}

}

#endif/* SHContext_h */

=========================

===================================================

控制器2:SHSnowView.m

//

//  SHSnowView.m

//  SnowAnimation(下雪动画)~demo

//

//  Created by石虎on 2017/8/15.

//  Copyright © 2017年shihu. All rights reserved.

//

#import"SHSnowView.h"

#import"SHContext.h"

@implementationSHSnowView

//定义雪花的初始化位置

staticCGPointsnowPos[] = {

{20,4},

{50,4},

{80,4},

{110,4},

{140,4},

{170,4},

{200,4},

{230,4},

{260,4},

{290,4}

};

//计算雪花的数量

staticNSIntegersnowCount =sizeof(snowPos)

/sizeof(snowPos[0]);

-(id) initWithCoder:(NSCoder*)aDecoder

{

self= [superinitWithCoder:aDecoder];

if(self)

{

//控制每隔0.2秒执行一次setNeedsDisplay方法刷新自己

[NSTimerscheduledTimerWithTimeInterval:0.2target:self

selector:@selector(setNeedsDisplay)userInfo:nilrepeats:YES];

}

returnself;

}

- (void)drawRect:(CGRect)rect

{

CGContextRefctx =UIGraphicsGetCurrentContext();

//设置采用白色作为填充色

CGContextSetRGBFillColor(ctx ,1,1,1,1);

for(inti =0; i

{

//保存当前绘图状态

CGContextSaveGState(ctx);

//平移坐标系统

CGContextTranslateCTM(ctx ,snowPos[i].x,snowPos[i].y);

//旋转坐标系统

CGContextRotateCTM(ctx , (arc4random() %6-3) *M_PI/10);

//控制“雪花”下掉

snowPos[i].y+=arc4random() %8;

if(snowPos[i].y>320)

{

snowPos[i].y =4;

}

//创建、并绘制“雪花”

CGContextAddFlower(ctx ,6,0,0,4,8);

CGContextFillPath(ctx);

//恢复绘图状态

CGContextRestoreGState(ctx);

}

}

@end

================

=======

谢谢!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容