您的位置:首页 > 移动开发 > IOS开发

ios学习笔记---用View动画仿UC浏览器菜单栏弹出效果

2013-07-05 19:05 706 查看
本人大二学生一枚,之前有学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];


也就是说ios的动画就是在几行代码之间完成的,begin后就是动画的开始,commit就是动画的结束,实际上是委派给UIview内部处理,当然这是我个人理解,有异议请指教。
先看看这个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 下载

今天就到这吧~····好好学习···天天向上~!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐