歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

iPhone九宮格的實現

看到很多同學在問,其實很簡單,我這是用圖片堆砌實現的九宮格,分享如下:

效果圖:

核心就這2個方法:

[plain]

  1. //Power by ieliwb.com  
  2. - (void)viewDidLoad {  
  3.     [super viewDidLoad];  
  4.       
  5.     NSArray* imageNames = [NSArray arrayWithObjects:  
  6.                                         @"ico_mobile.png",   
  7.                                         @"ico_idcard.png",   
  8.                                         @"ico_postcode.png",  
  9.                                         @"ico_flight.png",   
  10.                                         @"ico_translate.png",  
  11.                                         @"ico_phone.png",   
  12.                                         @"ico_car.png",   
  13.                                         @"ico_health.png",   
  14.                                         @"ico_bjxm.png", nil];  
  15.    
  16.     UIButton *Btn;  
  17.     for (int i=0; i<9; i++) {  
  18.         CGRect frame;  
  19.         Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];  
  20.         [Btn setImage:[UIImage imageNamed:[imageNames objectAtIndex: i]]forState:UIControlStateNormal];//設置按鈕圖片  
  21.           
  22.         Btn.tag = i;  
  23.           
  24.         frame.size.width = 59;//設置按鈕坐標及大小  
  25.         frame.size.height = 75;  
  26.         frame.origin.x = (i%3)*(59+32)+40;  
  27.         frame.origin.y = floor(i/3)*(75+24)+40;  
  28.         [Btn setFrame:frame];  
  29.           
  30.         [Btn setBackgroundColor:[UIColor clearColor]];  
  31.         [Btn addTarget:self action:@selector(btnPressed:)forControlEvents:UIControlEventTouchUpInside];  
  32.         [self.view addSubview:Btn];  
  33.         [Btn release];  
  34.           
  35.     }  
  36.           
  37. }  
  38.    
  39. //響應按鈕事件  
  40. -(void)btnPressed:(id)sender{  
  41.     UIButton *Btn = (UIButton *)sender;  
  42.         int index = Btn.tag;  
  43.       
  44.     switch (index) {  
  45.         case 0:  
  46.             if(mobileController==nil)  
  47.                 mobileController = [[MobileController alloc]init];  
  48.             [self.navigationController pushViewController:mobileControlleranimated:YES];  
  49.             break;  
  50.         //其他幾個控制器類似  
  51.     }  
  52.    
  53. }  
九宮格背景修改可以這樣實現:

[plain]

  1. - (void)loadView {  
  2.     UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]];  
  3.     [contentView setImage:[UIImage imageNamed:@"subview_9_bg.png"]];  
  4.     [contentView setUserInteractionEnabled:YES];  
  5.     self.view = contentView;  
  6.     [contentView release];  
  7. }  
  8. UINavigationBar背景圖片可以這樣實現:  
  9.   
  10. @implementation UINavigationBar (CustomImage)  
  11. - (void)drawRect:(CGRect)rect {  
  12.     UIImage *image = [UIImage imageNamed: @"top_bg.png"];  
  13.     [image drawInRect:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)];  
  14. }  
  15. @end  
Copyright © Linux教程網 All Rights Reserved