您的位置:首页 > 其它

导航条的简单实现

2014-03-13 20:25 99 查看




#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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: