標籤:
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 雪花效果