UIView动画
2015-09-23 20:00
411 查看
// // RootViewController.m // LessonUIView-01 // // Created by lanou3g on 15/9/23. // Copyright (c) 2015年 山神. All rights reserved. // #import "RootViewController.h" #import "UIView+WLFrame.h" @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 addSomeViews]; } - (void)addSomeViews { self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)] autorelease]; self.imageView.backgroundColor = [UIColor greenColor]; [self.view addSubview:self.imageView]; self.center = self.imageView.center; self.transform = self.imageView.transform; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 250, 100, 50); button.backgroundColor = [UIColor redColor]; [button addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; button1.frame = CGRectMake(100, 350, 100, 50); button1.backgroundColor = [UIColor redColor]; [button1 addTarget:self action:@selector(actionButtonBlock:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button1]; } - (void)actionButton:(UIButton *)button { // UIView动画 // 特点: 全是类方法调用, 开始与结束之间的部分是动画改变的部分 // 动画大小, 位置, 颜色, 透明度等 // 动画开始: 参数1:标示符(名字); 参数2:携带参数 [UIView beginAnimations:@"UIView" context:nil]; // 设置动画 [UIView setAnimationDuration:0.5]; //设置时间:在多少秒钟完结动画 [UIView setAnimationDelay:0]; // 设置动画延迟 [UIView setAnimationRepeatAutoreverses:YES]; // 设置反转 // 设置代理 [UIView setAnimationDelegate:self]; // 设置代理方法 [UIView setAnimationWillStartSelector:@selector(WillStart)]; [UIView setAnimationDidStopSelector:@selector(DidStop)]; // 设置速度曲线 [UIView setAnimationCurve:UIViewAnimationCurveLinear]; // 循环次数 [UIView setAnimationRepeatCount:1]; // 设置持续动画 [UIView setAnimationBeginsFromCurrentState:YES]; self.imageView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0]; // 改变颜色, 透明度 self.imageView.center = CGPointMake(300, 300); // 改变位置 CGRect frame = self.imageView.frame; frame.size = CGSizeMake(200, 200); self.imageView.frame = frame; button.userInteractionEnabled = NO; button.tag = 111; // 动画提交 [UIView commitAnimations]; } // UIView动画的block方法 - (void)actionButtonBlock:(UIButton *)button { // button.userInteractionEnabled = NO; // block方法1 // UIView动画的block方法 [UIView animateWithDuration:1 animations:^{ // 执行动画 self.imageView.center = CGPointMake(300, 300); }]; // block方法2 // 参数3:该block写动画结束后要干的事 [UIView animateWithDuration:1 animations:^{ // 执行动画 // 2D仿射变换 transform形变属性 // 平移 // 参数1:要形变的view的transform // [UIView setAnimationRepeatAutoreverses:YES]; // 如果添加反转属性, 就不用添加block了 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) { button.userInteractionEnabled = YES; }]; }]; #pragma mark - 缩放 [UIView animateWithDuration:0.5 animations:^{ // 缩放: 参数2和3:缩放的比例. [UIView setAnimationRepeatAutoreverses:YES]; self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 2, 2); } completion:^(BOOL finished) { self.imageView.transform = self.transform; button.userInteractionEnabled = YES; }]; #pragma mark - 旋转 // 需求: 点击按钮一直转, 再点就停 [UIView animateWithDuration:0.00001 animations:^{ // 旋转 参数2:旋转的角度 self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, 0.15); } completion:^(BOOL finished) { // 结束时调用循环转方法 [self rotateAnimation]; }]; self.isRuning = !self.isRuning; } // 循环转 - (void)rotateAnimation { // 旋转 // 参数2 旋转的角度 if (self.isRuning == YES) { [UIView animateWithDuration:0.01 animations:^{ // 旋转 // 参数2 旋转的角度 self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, 0.35); // 给一个转的初值 self.isRuning = YES; } completion:^(BOOL finished) { [self rotateAnimation]; }]; } } #pragma mark - 实现代理方法 - (void)WillStart { NSLog(@"动画开始"); } - (void)DidStop { NSLog(@"动画停止"); // 复原初始位置 self.imageView.center = self.center; UIButton *button = (UIButton *)[self.view viewWithTag:111]; self.imageView.backgroundColor = [UIColor greenColor]; self.imageView.frame = CGRectMake(100, 100, 100, 100); button.userInteractionEnabled = YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
相关文章推荐
- Number Sequence
- request.getInputStream中文乱码解决方案
- iOS UITextView 光标问题
- IOS UILabel
- 【转】有了Auto Layout,为什么你还是害怕写UITabelView的自适应布局?
- 自定义UITableViewCell 上的多个按钮点击事件处理
- UICollectionView基础
- quick减少包体
- 传感器 -UIAccelerometer
- iOS-Core-Animation-Advanced-Techniques(六)
- 数莓派kernel building官方文档
- /res/values 目录详解
- DRUID连接池的使用——为监控而生的DB池[转]
- CYC-非常炫酷的UIView动画-02
- NGUI控件
- UI 框架、ORM、权限系统
- easyui_layout折叠
- UITableView 获取多选,单选的内容
- UI的布局优化
- UITableView