iPhone的九宫格实现代码(转)
来源:互联网 发布:阿里云os系统手机 编辑:程序博客网 时间:2024/04/30 05:47
iPhone的九宫格实现代码
本文转载自:http://www.ieliwb.com/iphone-nine-box/
看到CocoaChina很多同学在问,其实很简单,我这是用图片堆砌实现的九宫格,分享如下:
效果图:
核心就这2个方法:
//Power by ieliwb.com
- (void)viewDidLoad {
[super viewDidLoad];
NSArray* imageNames = [NSArray arrayWithObjects:
@"ico_mobile.png",
@"ico_idcard.png",
@"ico_postcode.png",
@"ico_flight.png",
@"ico_translate.png",
@"ico_phone.png",
@"ico_car.png",
@"ico_health.png",
@"ico_bjxm.png", nil];
UIButton *Btn;
for (int i=0; i<9; i++) {
CGRect frame;
Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[Btn setImage:[UIImage imageNamed:[imageNames objectAtIndex: i]] forState:UIControlStateNormal];//设置按钮图片
Btn.tag = i;
frame.size.width = 59;//设置按钮坐标及大小
frame.size.height = 75;
frame.origin.x = (i%3)*(59+32)+40;
frame.origin.y = floor(i/3)*(75+24)+40;
[Btn setFrame:frame];
[Btn setBackgroundColor:[UIColor clearColor]];
[Btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Btn];
[Btn release];
}
}
//响应按钮事件
-(void)btnPressed:(id)sender{
UIButton *Btn = (UIButton *)sender;
int index = Btn.tag;
switch (index) {
case 0:
if(mobileController==nil)
mobileController = [[MobileController alloc]init];
[self.navigationController pushViewController:mobileController animated:YES];
break;
//其他几个控制器类似
}
}
- (void)viewDidLoad {
[super viewDidLoad];
NSArray* imageNames = [NSArray arrayWithObjects:
@"ico_mobile.png",
@"ico_idcard.png",
@"ico_postcode.png",
@"ico_flight.png",
@"ico_translate.png",
@"ico_phone.png",
@"ico_car.png",
@"ico_health.png",
@"ico_bjxm.png", nil];
UIButton *Btn;
for (int i=0; i<9; i++) {
CGRect frame;
Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[Btn setImage:[UIImage imageNamed:[imageNames objectAtIndex: i]] forState:UIControlStateNormal];//设置按钮图片
Btn.tag = i;
frame.size.width = 59;//设置按钮坐标及大小
frame.size.height = 75;
frame.origin.x = (i%3)*(59+32)+40;
frame.origin.y = floor(i/3)*(75+24)+40;
[Btn setFrame:frame];
[Btn setBackgroundColor:[UIColor clearColor]];
[Btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Btn];
[Btn release];
}
}
//响应按钮事件
-(void)btnPressed:(id)sender{
UIButton *Btn = (UIButton *)sender;
int index = Btn.tag;
switch (index) {
case 0:
if(mobileController==nil)
mobileController = [[MobileController alloc]init];
[self.navigationController pushViewController:mobileController animated:YES];
break;
//其他几个控制器类似
}
}
九宫格背景修改可以这样实现:
- (void)loadView {
UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:@"subview_9_bg.png"]];
[contentView setUserInteractionEnabled:YES];
self.view = contentView;
[contentView release];
}
UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:@"subview_9_bg.png"]];
[contentView setUserInteractionEnabled:YES];
self.view = contentView;
[contentView release];
}
UINavigationBar背景图片可以这样实现:
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"top_bg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"top_bg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
- iPhone的九宫格实现代码(转)
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- iPhone的九宫格实现代码
- 实现Android九宫格布局的代码
- Android:GirdView实现九宫格的代码
- iphone开发中九宫格的界面布局的实现
- iphone开发中九宫格的界面布局的实现
- iphone九宫格的界面
- 【转载】iphone九宫格界面实现
- 【转】高性能服务器架构(High-Performance Server Architecture)
- HashMap的设计理念
- 在iPhone开发中关于子UIViewController和父UIViewController之间相互调用方法的办法
- 非iDP(没有$99证书)把app部署到iPhone上的方法
- iphone获取GPS的数据信息并定位
- iPhone的九宫格实现代码(转)
- 我要资源分
- handle(句柄)的作用
- 高效使用JavaEE ORM框架
- filter
- Flex Viewer 解析(二)Flex Viewer源码包结构
- binder分析
- IE和Firefox完全兼容的Tab代替Enter的Javascript代码
- soso