本文實例為大家分享了IOSdrawRect實現雪花飄落效果的具體代碼,供大家參考,具體內容如下
繪制原理:
雪花效果最主要的思路就是在于循環產生帶雪花圖片的imageView, 產生的雪花的imageview的 x、y、寬、下落的速度都是隨機的,這個可以用隨機數來產生數據。
實現代碼:
#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
#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
#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
運行效果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答