本文實(shí)例為大家分享了IOSdrawRect實(shí)現(xiàn)雪花飄落效果的具體代碼,供大家參考,具體內(nèi)容如下
繪制原理:
雪花效果最主要的思路就是在于循環(huán)產(chǎn)生帶雪花圖片的imageView, 產(chǎn)生的雪花的imageview的 x、y、寬、下落的速度都是隨機(jī)的,這個(gè)可以用隨機(jī)數(shù)來產(chǎn)生數(shù)據(jù)。
實(shí)現(xiàn)代碼:
#import <UIKit/UIKit.h>@interface HHFSnowflakeFallingView : UIView/** * 快速創(chuàng)建一個(gè)雪花飄落效果的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{ //啟動(dòng)定時(shí)器,使得一直調(diào)用setNeedsDisplay從而調(diào)用- (void) drawRect:(CGRect )rect //不得手動(dòng)調(diào)用- (void) drawRect:(CGRect )rect CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)]; //讓定時(shí)器循環(huán)調(diào)用 [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];}- (void) drawRect:(CGRect)rect { //控制雪花最多的個(gè)數(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è)置動(dòng)畫 [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è)置雪花透明度,使得雪花快落地的時(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ī)的,這個(gè)可以用隨機(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í)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選