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



  • 相关阅读:
    VC编译器
    位域
    位域2
    函数调用时刻的堆栈情况
    字节对齐2
    Python学习笔记(6)while循环、while嵌套
    Python学习笔记(7)字符串Str
    Python学习笔记(3)输出、输入、输出输入综合小练习
    Python学习笔记(1)Python介绍、解释器、第一个python程序、注释
    Python学习笔记(4)运算符、运算符小练习
  • 原文地址:https://www.cnblogs.com/foxmin/p/2410169.html
Copyright © 2011-2022 走看看