zoukankan      html  css  js  c++  java
  • iPhone开发之九宫格实现(一)

    总体思路:用图片定位。

    //
    // ViewController.m
    // SquaresDemo
    //
    // Created by Fox on 12-3-21.
    // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
    //

    #import "ViewController.h"
    #import "NextView.h"

    NextView *nextView;

    @implementation ViewController


    - (void)viewDidLoad
    {
    [super viewDidLoad];
    //将每个图片作为一格,通过控制它的位置来实现九宫格。
    NSArray* imageNames = [NSArray arrayWithObjects:
    @"1.png",
    @"1.png",
    @"1.png",
    @"1.png",
    @"1.png",
    @"1.png",
    @"1.png",
    @"1.png",
    @"1.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)viewDidUnload
    {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }

    //响应按钮事件
    -(void)btnPressed:(id)sender{
    NSLog(@"btnpress!");
    UIButton *Btn = (UIButton *)sender;
    int index = Btn.tag;

    switch (index) {
    case 0:
    NSLog(@"2222");
    if(nextView==nil)
    nextView = [[NextView alloc]init];
    [self.navigationController pushViewController:nextView animated:YES];
    break;
    //其他几个控制器类似
    default:
    if(nextView==nil)
    nextView = [[NextView alloc]init];
    [self.navigationController pushViewController:nextView animated:YES];

    }

    }

    @end



  • 相关阅读:
    python闭包&深浅拷贝&垃圾回收&with语句
    Python基本数据类型
    面向对象
    四则运算2
    周总结01
    软件工程个人作业01
    java web中乱码的种类和一些解决方式
    java web 增加信息课堂测试00
    课程00作业
    动手动脑07
  • 原文地址:https://www.cnblogs.com/foxmin/p/2410169.html
Copyright © 2011-2022 走看看