导航条的简单实现

 


#import "RootViewController.h"

@interface RootViewController ()
{
    UIView *view;
    UIView *view1;
    UILabel *templabel;
    UIScrollView *scroll;
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //创建UIScrollView
    scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 40)];
    scroll.backgroundColor=[UIColor whiteColor];
    scroll.scrollEnabled=YES;
    scroll.contentSize=CGSizeMake(600, 41);
    [self.view addSubview:scroll];
    [scroll release];
    
     //添加Label
    NSArray *array=[[NSArray alloc]initWithObjects:@"经济",@"财经",@"NBA",@"汽车",@"热点",@"房产",@"科技",@"娱乐",@"美女",@"军事", nil];
    for (int i=0; i<10; i++) {
        NSString *str=[array objectAtIndex:i];
        UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(change:)];
        
         [self creatLabel:CGRectMake(60*i, 0, 60, 40) color:[UIColor whiteColor] superView:scroll title:str action:tapGesture];
    }
    //添加view,必须在label创建之后
    //view=[[UIView alloc]initWithFrame:CGRectMake(10, 30, 40,5)];
    view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    view.backgroundColor=[UIColor grayColor];
    view.alpha=0.3;
    [scroll addSubview:view];
    
    view1=[[UIView alloc]initWithFrame:CGRectMake(0, 30, 50, 5)];
    view1.backgroundColor=[UIColor redColor];
    view1.alpha=1;
    [scroll addSubview:view1];

}
-(void)creatLabel:(CGRect)Frame color:(UIColor*)color superView:(UIView *)superView title:(NSString *)title action:(UITapGestureRecognizer *)actionName
{
   UILabel *label=[[UILabel alloc]initWithFrame:Frame];
    label.userInteractionEnabled=YES;
    label.textAlignment=NSTextAlignmentCenter;
    label.backgroundColor=color;
    [superView addSubview:label];
    [label release];
    [label addGestureRecognizer:actionName];
    [actionName release];
    label.text=title;
}
//NSInteger i=0;

- (void)change:(UITapGestureRecognizer *)tap
{
    view.frame=tap.view.frame;
    view1.center=CGPointMake(tap.view.center.x,tap.view.center.y+15);
    
    //    count++;
//    if (count==1) {
//        templabel=(UILabel *)(tap.view);
//       
//    }
//    if (count>1) {
//        templabel.backgroundColor=[UIColor whiteColor];
//    }
//    UILabel *curlabel=(UILabel *)(tap.view);
//    curlabel.backgroundColor=[UIColor brownColor];
//    templabel=curlabel;
  
}
-(void)dealloc
{
    [view release];
    [view1 release];
   [templabel release];
    [scroll release];
    [super dealloc];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值