iPhone开发九宫格代码实现

移动开发 iOS
本文介绍的是iPhone开发九宫格代码实现,讲述了九宫格的是实现代码,先来看详细内容。

iPhone开发九宫格代码实现是本文要介绍的内容,九宫格大家应该很感兴趣吧?那么本文就是来介绍九宫格的制作方法,先来看详细内容。看到CocoaChina很多同学在问,其实很简单,我这是用图片堆砌实现的九宫格,分享如下,代码如下。

效果图:

iPhone开发九宫格代码实现

核心就这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;  
        //其他几个控制器类似  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

九宫格背景修改可以这样实现:

- (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];  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

小结:iPhone开发九宫格代码实现的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2015-01-26 13:22:55

密码锁

2019-03-24 15:05:23

手机安全解锁

2013-05-29 10:44:11

2015-03-05 15:27:22

2018-08-14 13:34:30

商业模式

2021-12-31 08:12:05

微信微信支付云闪付

2013-07-30 11:16:33

iOS 7 Beta 九宫格输入法

2011-09-16 10:35:13

Android应用数独经典游戏

2022-04-21 21:49:57

元宇宙

2023-04-20 08:26:17

九宫格布局客户端

2020-09-16 12:10:19

九宫格视频Python文件

2022-03-03 18:49:56

HarmonyOS鸿蒙操作系统

2017-03-29 15:41:45

互联网

2013-10-31 11:19:09

微软语音翻译手语翻译

2011-05-25 19:35:25

2012-04-13 16:35:02

傲游手机浏览器发布

2021-10-12 16:39:39

支付宝口碑移动应用

2019-08-06 16:40:33

手机按键屏幕

2013-07-30 14:25:51

苹果iOS 7iTunes 11.1

2023-05-31 10:08:51

点赞
收藏

51CTO技术栈公众号