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

本人大二学生一枚,之前有学android,做了几个项目,现在没事学学ios~···

最近在做一个简单的项目,需要实现类似UC浏览器菜单栏弹出的动画,在网上找了半天。没发现有类似的demo,于是就自己写了个简单的demo。关于ios View的动画我在这里就简单介绍下,详细请移步高手总结的文档http://www.cnblogs.com/larryblog/archive/2012/08/02/2620731.html

ios的动画框架如下:

[cpp]  view plain copy
  1. [UIView beginAnimations:nil context:nil]; //动画开始  
  2.   
  3. [UIView setAnimationDuration:2]; //动画持续时间  
  4. //动画内容  
  5. frame.origin.x += 150;  
  6.   
  7. [img setFrame:frame];  
  8. //动画结束  
  9. [UIView commitAnimations];  



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

先看看这个demo的运行结果



话不多说了,直接贴demo的代码吧;详情看注释:

.h文件就省略了

直接贴.m文件吧

[cpp]  view plain copy
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8. @synthesize UCMenu;  
  9. @synthesize button;  
  10. - (void)viewDidLoad  
  11. {  
  12.     [super viewDidLoad];  
  13.     //初始化MenuView  
  14.     UCMenu = [[UIView alloc]init];  
  15.     CGRect rect = CGRectMake(0, 548, 320, 100);  
  16.     UCMenu.backgroundColor = [UIColor grayColor];  
  17.     UCMenu.alpha = 0;  
  18.     UCMenu.frame = rect;  
  19.       
  20.     //向View中添加按钮图标  
  21.     NSArray *array = [[NSArray alloc]initWithObjects:@"setting.png",@"search.png",@"spanner.png",@"tick.png",@"ticket.png",@"preferences.png", nil];  
  22.     int j = 0;  
  23.     for (int i = 0 ; i < 2; i++) {  
  24.         for (int k = 0 ; k < 3; k ++) {  
  25.             UIButton *buttonStep = [[UIButton alloc]init];  
  26.             UIImage *image = [UIImage imageNamed:[array objectAtIndex:j]];  
  27.              
  28.             [buttonStep setBackgroundImage:image forState:UIControlStateNormal];  
  29.             CGRect rect = CGRectMake(48 + k * 85, i * 48 , 48, 48);  
  30.             buttonStep.frame = rect;  
  31.             //[buttonStep setTitle:@"aadf" forState:UIControlStateNormal];  
  32.             [UCMenu addSubview:buttonStep];  
  33.             j++;  
  34.   
  35.         }  
  36.           
  37.     }  
  38.       
  39.     button = [UIButton buttonWithType:UIButtonTypeRoundedRect];         //弹出\关闭菜单按钮  
  40.     CGRect buttonRect = CGRectMake(103, 86, 115, 43);  
  41.     button.frame = buttonRect;  
  42.     [button setTitle:@"点我弹出菜单" forState:UIControlStateNormal];  
  43.     [button addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];  
  44.     [self.view addSubview:button];  
  45.       
  46.     [self.view addSubview:UCMenu];  
  47.     // Do any additional setup after loading the view, typically from a nib.  
  48. }  
  49. //弹出菜单窗口  
  50. - (IBAction)showMenu:(id)sender  
  51. {  
  52.     [UIView beginAnimations:nil context:nil];   //动画开始  
  53.     [UIView setAnimationDuration:0.5];          //动画持续时间  
  54.     CGRect rect = UCMenu.frame;                 //得到当前frame  
  55.     rect.origin.y -= 100;                       //改变y坐标(向上移动)  
  56.     UCMenu.frame = rect;                        //重新传回View  
  57.     UCMenu.alpha = 1;                           //透明度从0到1  
  58.     [UIView commitAnimations];                  //动画结束  
  59.       
  60.     //改变按钮的点击事件和title  
  61.     [button setTitle:@"点我关闭菜单" forState:UIControlStateNormal];  
  62.     [button removeTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];  
  63.     [button addTarget:self action:@selector(dismissMenu:) forControlEvents:UIControlEventTouchUpInside];  
  64. }  
  65. //关闭菜单窗口  
  66. - (IBAction)dismissMenu:(id)sender  
  67. {  
  68.     [UIView beginAnimations:nil context:nil];   //动画开始  
  69.     [UIView setAnimationDuration:0.3];          //动画持续时间  
  70.     CGRect rect = UCMenu.frame;                 //得到当前frame  
  71.     rect.origin.y += 100;                       //改变y坐标(向下移动)  
  72.     UCMenu.frame = rect;                         //重新传回View  
  73.     UCMenu.alpha = 0;                           //透明度从1到0  
  74.     [UIView commitAnimations];                  //动画结束  
  75.       
  76.     //改变按钮的点击事件和title  
  77.     [button removeTarget:self action:@selector(dismissMenu:) forControlEvents:UIControlEventTouchUpInside];  
  78.     [button addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];  
  79.     [button setTitle:@"点我弹出菜单" forState:UIControlStateNormal];  
  80. }  
  81.   
  82. - (void)didReceiveMemoryWarning  
  83. {  
  84.     [super didReceiveMemoryWarning];  
  85.     // Dispose of any resources that can be recreated.  
  86. }  
  87.   
  88. @end  


完整工程移步   http://download.csdn.net/detail/kekeqiaokeli/5707955     下载

今天就到这吧~····好好学习···天天向上~!

转自:http://blog.csdn.net/kekeqiaokeli/article/details/9254813

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值