iOS 雪花效果

來源:互聯網
上載者:User

標籤:

1.transform屬性介紹

在OC中,通過transform屬性可以修改對象的平移、縮放比例和旋轉角度

常用的建立transform結構體方法分兩大類

  (1) 建立“基於控制項初始位置”的形變

  CGAffineTransformMakeTranslation(平移)

  CGAffineTransformMakeScale(縮放)

  CGAffineTransformMakeRotation(旋轉)

 (2) 建立“基於transform參數”的形變

  CGAffineTransformTranslate

  CGAffineTransformScale

  CGAffineTransformRotate

 2. 代碼實現
#import "ViewController.h"#define degree2angle(angle)  ((angle) * M_PI / 180)
@interface ViewController ()@property (nonatomic, strong) CADisplayLink *timer;@property (nonatomic ,assign) long long steps;@end@implementation ViewController- (CADisplayLink *)timer{ if (_timer == nil) { _timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)]; } return _timer;}- (void)loadView{ UIImageView *bg = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; bg.image = [UIImage imageNamed:@"snowbg.jpg"]; bg.contentMode = UIViewContentModeScaleAspectFill; bg.userInteractionEnabled = YES; self.view = bg;}
- (void)viewDidLoad { [super viewDidLoad]; [self.timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];}
//點擊的時候動畫停止- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self stopTimer];}- (void)stopTimer{ [self.timer invalidate];}- (void)update{ _steps++; //執行5次添加一次 if (_steps % 5 == 0) { [self snow]; }}// 隱藏狀態列- (BOOL)prefersStatusBarHidden{ return YES;}//方法一 用transform屬性設定/*- (void)snow{ // 1.執行個體化一個雪花的映像視圖 UIImage *image = [UIImage imageNamed:@"雪花"]; UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; // 0.1~0.4 隨機在0.01~0.4縮放 CGFloat scale = arc4random_uniform(40)/100.0 + 0.01; // 設定雪花縮放 imageView.transform = CGAffineTransformMakeScale(scale, scale);
CGSize winSize = self.view.bounds.size;
// 2.添加到視圖 CGFloat x = arc4random_uniform(winSize.width); CGFloat y = -imageView.bounds.size.height * 0.5; imageView.center = CGPointMake(x, y); [self.view addSubview:imageView]; // 3.動畫下落 [UIView animateWithDuration:10.0f animations:^{ // 移動到終點位置 CGFloat toX = arc4random_uniform(winSize.width); CGFloat toY = winSize.height + imageView.bounds.size.height * 0.5; imageView.center = CGPointMake(toX, toY); // 下過過程中,設定雪花360度隨機旋轉 imageView.transform = CGAffineTransformRotate(imageView.transform, degree2angle(arc4random_uniform(360))); imageView.alpha = 0.3; } completion:^(BOOL finished) { [imageView removeFromSuperview]; }];}*/ //方法二 用kvc設定- (void)snow{ // 1.執行個體化一個雪花的映像視圖 // 整個視圖的尺寸 CGSize winSize = self.view.bounds.size; UIImage *image = [UIImage imageNamed:@"雪花"];
// 2.添加到視圖 UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; CGFloat startX = arc4random_uniform(winSize.width); CGFloat startY = -imageView.bounds.size.height; imageView.center = CGPointMake(startX, startY); // 0.01~0.4 隨機在0.01~0.4縮放 CGFloat scale = arc4random_uniform(40)/100.0 + 0.01; [imageView.layer setValue:@(scale) forKeyPath:@"transform.scale"]; [self.view addSubview:imageView]; //3.動畫下落 [UIView animateWithDuration:10.0f animations:^{ // 移動到終點位置 CGFloat endX = arc4random_uniform(winSize.width); CGFloat endY = winSize.height + imageView.bounds.size.height; imageView.center = CGPointMake(endX, endY); // 下落的過程中旋轉180度 [imageView.layer setValue:@(M_PI) forKeyPath:@"transform.rotation"]; // 不透明度為0.3 [imageView.layer setValue:@(0.3) forKeyPath:@"opacity"]; } completion:^(BOOL finished) { // 結束動畫移除imageView [imageView removeFromSuperview]; }]; }@end

 

iOS 雪花效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.