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

本人大二学生一枚,之前有学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     下载

今天就到这吧~····好好学习···天天向上~!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值