IOSdrawRect實(shí)現(xiàn)雪花飄落效果
本文實(shí)例為大家分享了IOSdrawRect實(shí)現(xiàn)雪花飄落效果的具體代碼,供大家參考,具體內(nèi)容如下
繪制原理:
雪花效果最主要的思路就是在于循環(huán)產(chǎn)生帶雪花圖片的imageView, 產(chǎn)生的雪花的imageview的 x、y、寬、下落的速度都是隨機(jī)的,這個可以用隨機(jī)數(shù)來產(chǎn)生數(shù)據(jù)。
實(shí)現(xiàn)代碼:
#import <UIKit/UIKit.h> @interface HHFSnowflakeFallingView : UIView /** * 快速創(chuàng)建一個雪花飄落效果的view * * @param bgImageName 背景圖片的名稱 * @param snowImageName 雪花圖片的名稱 * @param frame frame * * @return 實(shí)例化的 雪花飄落效果的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{
//啟動定時器,使得一直調(diào)用setNeedsDisplay從而調(diào)用- (void) drawRect:(CGRect )rect
//不得手動調(diào)用- (void) drawRect:(CGRect )rect
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
//讓定時器循環(huán)調(diào)用
[link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
- (void) drawRect:(CGRect)rect {
//控制雪花最多的個數(shù)
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;
}
//雪花起點(diǎn)y
int startY = - (arc4random() % 100);
//雪花起點(diǎn)x
int startX = arc4random() % (int) [UIScreen mainScreen].bounds.size.width;
//雪花終點(diǎn)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];
//設(shè)置動畫
[UIView animateWithDuration:speed animations:^{
//設(shè)置雪花最終的frame
imageView.frame = CGRectMake(endX, [UIScreen mainScreen].bounds.size.height, width, width);
//設(shè)置雪花的旋轉(zhuǎn)
imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI);
//設(shè)置雪花透明度,使得雪花快落地的時候就像快消失的一樣
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];
/**
雪花效果最主要的思路就是在于循環(huán)產(chǎn)生帶雪花圖片的imageView, 產(chǎn)生的雪花的imageview的 x、y、寬、下落的速度都是隨機(jī)的,這個可以用隨機(jī)數(shù)來產(chǎn)生數(shù)據(jù)。
*/
self.navigationItem.title = @"雪花飄落效果";
//創(chuàng)建雪花飄落效果的view
HHFSnowflakeFallingView *snowflakeFallingView = [HHFSnowflakeFallingView snowfladeFallingViewWithBackgroundImageName:@"snow_background" snowImageName:@"snow" initWithFrame:self.view.bounds];
//開始下雪
[snowflakeFallingView beginShow];
[self.view addSubview:snowflakeFallingView];
}
@end
運(yùn)行效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)中仿Tumblr點(diǎn)贊心破碎動畫效果
這篇文章主要介紹了iOS開發(fā)中仿Tumblr點(diǎn)贊心破碎動畫效果,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-04-04
iOS開發(fā)中用imageIO漸進(jìn)加載圖片及獲取exif的方法
這篇文章主要介紹了iOS開發(fā)中中用imageIO漸進(jìn)加載圖片及獲取exif的方法,代碼演示為傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09
在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析
這篇文章主要介紹了在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析,包括獲取當(dāng)前位置和計(jì)算兩點(diǎn)間距離等基本功能的實(shí)現(xiàn),需要的朋友可以參考下2016-05-05
iOS程序開發(fā)之使用PlaceholderImageView實(shí)現(xiàn)優(yōu)雅的圖片加載效果
這篇文章主要介紹了ioS程序開發(fā)之使用PlaceholderImageView實(shí)現(xiàn)優(yōu)雅的圖片加載效果的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
iOS開發(fā)中Quartz2D控制圓形縮放和實(shí)現(xiàn)刷幀效果
這篇文章主要介紹了iOS開發(fā)中Quartz2D控制圓形縮放和實(shí)現(xiàn)刷幀效果的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS開發(fā)中Subview的事件響應(yīng)以及獲取subview的方法
這篇文章主要介紹了iOS開發(fā)中Subview的事件響應(yīng)以及獲取subview的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09
iOS 使用UITextField自定義搜索框 實(shí)現(xiàn)用戶輸入完之后“實(shí)時搜索”功能
這篇文章主要介紹了iOS 使用UITextField自定義搜索框 實(shí)現(xiàn)用戶輸入完之后“實(shí)時搜索”功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

