标签:
在OC中,通过transform属性可以修改对象的平移、缩放比例和旋转角度
常用的创建transform结构体方法分两大类
(1) 创建“基于控件初始位置”的形变
CGAffineTransformMakeTranslation(平移)
CGAffineTransformMakeScale(缩放)
CGAffineTransformMakeRotation(旋转)
(2) 创建“基于transform参数”的形变
CGAffineTransformTranslate
CGAffineTransformScale
CGAffineTransformRotate
#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
标签:
原文地址:http://www.cnblogs.com/LXshmily/p/4674475.html