iOS SnowAnimation(下雪動畫)~demo

//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄

/**

注意點: 1.看 GIF 效果圖.

2.看連線視圖的效果圖.

3.看實現(xiàn)代碼(直接復制實現(xiàn)效果).

*/

一、GIF 效果圖:

二、連線視圖的效果圖:

圖1:

圖2:

圖3:

三、實現(xiàn)代碼:

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

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

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

控制器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:該參數(shù)通常應設為奇數(shù),控制繪制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:該參數(shù)控制花朵的花瓣數(shù)。

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

size:控制花朵的大小

length:控制花瓣的長度

*/

voidCGContextAddFlower(CGContextRefc ,NSIntegern

,CGFloatdx ,CGFloatdy ,CGFloatsize ,CGFloatlength)

{

//移動到指定點

CGContextMoveToPoint(c , dx , dy + size);

CGFloatdig =2*M_PI/ n;

//采用循環(huá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}

};

//計算雪花的數(shù)量

staticNSIntegersnowCount =sizeof(snowPos)

/sizeof(snowPos[0]);

-(id) initWithCoder:(NSCoder*)aDecoder

{

self= [superinitWithCoder:aDecoder];

if(self)

{

//控制每隔0.2秒執(zhí)行一次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

{

//保存當前繪圖狀態(tài)

CGContextSaveGState(ctx);

//平移坐標系統(tǒng)

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

//旋轉坐標系統(tǒng)

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

//控制“雪花”下掉

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

if(snowPos[i].y>320)

{

snowPos[i].y =4;

}

//創(chuàng)建、并繪制“雪花”

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

CGContextFillPath(ctx);

//恢復繪圖狀態(tài)

CGContextRestoreGState(ctx);

}

}

@end

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

=======

謝謝!!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容