@interface RootViewController ()
// 声明一个imageView
@property (nonatomic, retain) UIImageView *imageView;
// 保存一下初始的中心点
@property (nonatomic, assign) CGPoint center;
// 声明形变属性
@property (nonatomic, assign) CGAffineTransform transform;
// 声明一个属性
@property (nonatomic, assign) BOOL isRuning;
@end
@implementation RootViewController
- (void)dealloc
{
[_imageView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addSubViews];
}
- (void)addSubViews
{
self.imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(85, 100, 200, 200))];
self.imageView.backgroundColor = [UIColor colorWithRed:0.222 green:0.718 blue:1.000 alpha:1.000];
[self.view addSubview:self.imageView];
[_imageView release];
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(100, 400, 70, 50);
[button setTitle:@"点我" forState:(UIControlStateNormal)];
button.backgroundColor = [UIColor colorWithWhite:0.001 alpha:1.000];
[button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
button1.frame = CGRectMake(200, 400, 70, 50);
[button1 setTitle:@"点点" forState:(UIControlStateNormal)];
button1.backgroundColor = [UIColor colorWithWhite:0.001 alpha:1.000];
[button1 addTarget:self action:@selector(actionBlockButton:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button1];
// 保存一下中心点
_center = self.imageView.center;
// 记录一下形变的属性
_transform = self.imageView.transform;
}
// uiView 动画的block方法
- (void)actionBlockButton:(UIButton *)button
{
#pragma mark - 平移
// block 方法1
// [UIView animateWithDuration:1 animations:^{
//
// // 执行的动画
// self.imageView.center = CGPointMake(400, 400);
//
// }];
/*
// block 方法2
//参数3 该Block 写 动画结束后要干的事
[UIView animateWithDuration:1 animations:^
{
// 如果设置了反转属性 那么在结束后 不用零另行添加动画了
[UIView setAnimationRepeatAutoreverses:YES];
// 执行的动画
// 2D仿射变换 transform形变属性
// 平移
// 参数1 填要形变的View
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 100, 0);
} completion:^(BOOL finished)
{
// 上面的动画结束后 触发(相当于 代理方法 的完成动画方法)
// 复原位子
// [UIView animateWithDuration:1 animations:^{
// self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -100, 0);
// } completion:^(BOOL finished) {
//
// }];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -100, 0);
}];
*/
#pragma mark - 缩放
/*
[UIView animateWithDuration:1 animations:^
{
[UIView setAnimationRepeatAutoreverses:YES];
// 参数2 ,3 缩放的比例
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 2, 2);
} completion:^(BOOL finished)
{
// 复原
// self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 0.5,0.5);
self.imageView.transform = _transform;
}];
*/
#pragma mark - 旋转
// 需求
// 点击按钮 一直转 再点 停
// 需求
// 点击按钮 一直转 再点 停
[UIView animateWithDuration:0.1 animations:^
{
// 旋转
// 参数2 旋转的角度(直接填数字值是正传)
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);
} completion:^(BOOL finished)
{
[self rotateAnimation];
}];
}
// 循环转得方法
- (void)rotateAnimation
{
// 需求
// 点击按钮 一直转 再点 停
[UIView animateWithDuration:0.1 animations:^
{
// 旋转
// 参数2 旋转的角度(直接填数字值是正传)
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);
// 给转一个初值
_isRuning = YES;
} completion:^(BOOL finished)
{
[self rotateAnimation];
}];
}
- (void)actionButton:(UIButton *)button
{
// uiview 动画
// 特点: 全是类方法调用 开始与结束之间的部分 是动画改变的部分
// 动画大小 位置 颜色 透明度 等
// 动画开始
// 参数1 : 标示符 (名字)
// 参数2 携带的参数
[UIView beginAnimations:@"hah" context:nil];
// 设置动画
// 设置时间 在多少秒之内 完结动画
[UIView setAnimationDuration:1];
// 设置动画延迟
// [UIView setAnimationDelay:2];
//设置反转
[UIView setAnimationRepeatAutoreverses:YES];
// 设置代理
[UIView setAnimationDelegate:self];
// 代理方法
[UIView setAnimationWillStartSelector:@selector(viewStart)];
[UIView setAnimationDidStopSelector:@selector(viewStop)];
// 设置曲线
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
// 设置循环
[UIView setAnimationRepeatCount:3];
// 添加动画(改变位置)
self.imageView.center = CGPointMake(200, 300);
// 设置持续执行动画
[UIView setAnimationBeginsFromCurrentState:YES];
//改变颜色
self.imageView.backgroundColor = [UIColor colorWithRed:arc4random() %256 / 255.0 green:arc4random() %256 / 255.0 blue:arc4random() %256 / 255.0 alpha:1];
// 透明度
self.imageView.alpha = 0;
// 变大
CGRect frame = self.imageView.frame;
frame.size = CGSizeMake(300, 300);
self.imageView.frame = frame;
// 动画提交
[UIView commitAnimations];
}
#pragma mark - 自己设置代理方法
- (void)viewStart
{
NSLog(@"动画开始");
}
- (void)viewStop
{
NSLog(@"动画停止");
self.imageView.alpha = 1;
// 回复原来大小
CGRect frame = self.imageView.frame;
frame.size = CGSizeMake(200, 200);
self.imageView.frame = frame;
// 复原中心点
self.imageView.center = _center;
}
随波逐流之iOS Layer 详解
最新推荐文章于 2024-09-19 10:29:13 发布