本人大二学生一枚,之前有学android,做了几个项目,现在没事学学ios~···
最近在做一个简单的项目,需要实现类似UC浏览器菜单栏弹出的动画,在网上找了半天。没发现有类似的demo,于是就自己写了个简单的demo。关于ios View的动画我在这里就简单介绍下,详细请移步高手总结的文档http://www.cnblogs.com/larryblog/archive/2012/08/02/2620731.html
ios的动画框架如下:
- [UIView beginAnimations:nil context:nil]; //动画开始
- [UIView setAnimationDuration:2]; //动画持续时间
- //动画内容
- frame.origin.x += 150;
- [img setFrame:frame];
- //动画结束
- [UIView commitAnimations];
先看看这个demo的运行结果
话不多说了,直接贴demo的代码吧;详情看注释:
.h文件就省略了
直接贴.m文件吧
- #import "ViewController.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- @synthesize UCMenu;
- @synthesize button;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //初始化MenuView
- UCMenu = [[UIView alloc]init];
- CGRect rect = CGRectMake(0, 548, 320, 100);
- UCMenu.backgroundColor = [UIColor grayColor];
- UCMenu.alpha = 0;
- UCMenu.frame = rect;
- //向View中添加按钮图标
- NSArray *array = [[NSArray alloc]initWithObjects:@"setting.png",@"search.png",@"spanner.png",@"tick.png",@"ticket.png",@"preferences.png", nil];
- int j = 0;
- for (int i = 0 ; i < 2; i++) {
- for (int k = 0 ; k < 3; k ++) {
- UIButton *buttonStep = [[UIButton alloc]init];
- UIImage *image = [UIImage imageNamed:[array objectAtIndex:j]];
- [buttonStep setBackgroundImage:image forState:UIControlStateNormal];
- CGRect rect = CGRectMake(48 + k * 85, i * 48 , 48, 48);
- buttonStep.frame = rect;
- //[buttonStep setTitle:@"aadf" forState:UIControlStateNormal];
- [UCMenu addSubview:buttonStep];
- j++;
- }
- }
- button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //弹出\关闭菜单按钮
- CGRect buttonRect = CGRectMake(103, 86, 115, 43);
- button.frame = buttonRect;
- [button setTitle:@"点我弹出菜单" forState:UIControlStateNormal];
- [button addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button];
- [self.view addSubview:UCMenu];
- // Do any additional setup after loading the view, typically from a nib.
- }
- //弹出菜单窗口
- - (IBAction)showMenu:(id)sender
- {
- [UIView beginAnimations:nil context:nil]; //动画开始
- [UIView setAnimationDuration:0.5]; //动画持续时间
- CGRect rect = UCMenu.frame; //得到当前frame
- rect.origin.y -= 100; //改变y坐标(向上移动)
- UCMenu.frame = rect; //重新传回View
- UCMenu.alpha = 1; //透明度从0到1
- [UIView commitAnimations]; //动画结束
- //改变按钮的点击事件和title
- [button setTitle:@"点我关闭菜单" forState:UIControlStateNormal];
- [button removeTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
- [button addTarget:self action:@selector(dismissMenu:) forControlEvents:UIControlEventTouchUpInside];
- }
- //关闭菜单窗口
- - (IBAction)dismissMenu:(id)sender
- {
- [UIView beginAnimations:nil context:nil]; //动画开始
- [UIView setAnimationDuration:0.3]; //动画持续时间
- CGRect rect = UCMenu.frame; //得到当前frame
- rect.origin.y += 100; //改变y坐标(向下移动)
- UCMenu.frame = rect; //重新传回View
- UCMenu.alpha = 0; //透明度从1到0
- [UIView commitAnimations]; //动画结束
- //改变按钮的点击事件和title
- [button removeTarget:self action:@selector(dismissMenu:) forControlEvents:UIControlEventTouchUpInside];
- [button addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
- [button setTitle:@"点我弹出菜单" forState:UIControlStateNormal];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
完整工程移步 http://download.csdn.net/detail/kekeqiaokeli/5707955 下载
今天就到这吧~····好好学习···天天向上~!
转自:http://blog.csdn.net/kekeqiaokeli/article/details/9254813