iPhone開發九宮格代碼實現

來源:互聯網
上載者:User

iPhone開發九宮格代碼實現是本文要介紹的內容,九宮格大家應該高度興趣吧?那麼本文就是來介紹九宮格的製作方法,先來看詳細內容。看到CocoaChina很多同學在問,其實很簡單,我這是用圖片堆砌實現的九宮格,分享如下,代碼如下。

:

核心就這2個方法:

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

九宮格背景修改可以這樣實現:

 
  1. - (void)loadView {  
  2.     UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];  
  3.     [contentView setImage:[UIImage imageNamed:@"subview_9_bg.png"]];  
  4.     [contentView setUserInteractionEnabled:YES];  
  5.     self.view = contentView;  
  6.     [contentView release];  

UINavigationBar背景圖片可以這樣實現:

 
  1. @implementation UINavigationBar (CustomImage)  
  2. - (void)drawRect:(CGRect)rect {  
  3.     UIImage *image = [UIImage imageNamed: @"top_bg.png"];  
  4.     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];  
  5. }  
  6. @end 

小結:iPhone開發九宮格代碼實現的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.