11九宫格、Tom猫按钮

一、九宫格
1.1 ****方法****1 ****只能创建(****ij****)个对象*

#define WINDOW_WIDTH self.window.bounds.size.width
#define WINDOW_HEIGHT self.window.bounds.size.height
#define IMAGE_WIDTH 60
#define IMAGE_HEIGHT 60
#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //1.根据屏幕宽高,计算行间距、列间距
    float rowSpace = (WINDOW_HEIGHT - 3 * 60) / 4;
    float colSpace = (WINDOW_WIDTH - 3 * 60) / 4;
    
    for (int i = 0 ; i < 3; i++)
    {
        float y = rowSpace + i * (rowSpace + IMAGE_HEIGHT);
        for (int j = 0; j < 3; j++)
        {
            float x = colSpace + j * (colSpace + IMAGE_WIDTH);
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, IMAGE_WIDTH, IMAGE_HEIGHT)];
            imageView.backgroundColor = [UIColor redColor];
            [self.window addSubview:imageView];
        }
    }
    
    return YES;
}

1.2 ****方法****2 ****创建任意个对象

#define SCREEN_WIDTH [[UIScreen mainScreen]bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen]bounds].size.height
#define IMAGE_WIDTH 60
#define IMAGE_HEIGHT 60
#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //1.求行间距、列间距
    float rowSpace = (SCREEN_HEIGHT - 3 * IMAGE_HEIGHT)/4;
    float colSpace = (SCREEN_WIDTH - 3 * IMAGE_WIDTH)/4;
    
    //2.for循环
    //a:要创建的对象数
    for (int a = 0; a < 8; a++)
    {
        //1.计算行号和列号。注意:3代表多少列
        //1.1 行号:对3求整
        int i = a/3;
        //1.2 列号:对3求余
        int j = a%3;
        
        //2.计算x,y坐标
        float x = colSpace + j * (colSpace + IMAGE_WIDTH);
        float y = rowSpace + i * (rowSpace + IMAGE_HEIGHT);
        
        //3.创建图片
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, IMAGE_WIDTH, IMAGE_HEIGHT)];
        imgView.backgroundColor = [UIColor blueColor];
        [self.window addSubview:imgView];
    }
    
    return YES;
}

2.****创建****Tom****猫按钮
2.1 主函数

//1.添加图片视图,展示背景图片,同时实现帧动画效果
    _catImgView = [[UIImageView alloc]initWithFrame:self.window.frame];
    _catImgView.image = [UIImage imageNamed:@"angry_01.jpg"];
    [self.window addSubview:_catImgView];
    
    //2.将按钮的图片名字、图片数量,存放到数组中
    _btnImgNameArray = [[NSArray alloc]initWithObjects:@"cymbal",@"drink",@"eat",@"fart",@"pie",@"scratch", nil];
    _numberArry = [[NSArray alloc]initWithObjects:@"13",@"81",@"40",@"28",@"24",@"56", nil];
    
    //3.创建6个点击按钮
    for (int a = 0; a < 6; a++)
    {
        //1.列号 对2求余
        int i = a%2;
        float x = 0;
        //3目运算符,与下面if.else语句一样
//        float x = (i == 0) ? 30 : 230;
        if (i == 0)
        {
            x = 30;
        }
        else
        {
            x = 230;
        }
        //行号
        int j = a/2;
        float y = 0;
        y = 240 + j * (ROW_SPACE + BUTTON_HEIGHT);
        
        //根据索引值(a)取出图片名字
        NSString *name = [_btnImgNameArray objectAtIndex:a];
        
        //创建按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
        [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(animationClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = a;
        [self.window addSubview:button];
    }
    
    
    return YES;

2.2 ****代码封装

//将重复性的代码封装为方法
//参数:
//1.图片名
//2.图片数量
- (void)tomAnimationWithImageName:(NSString *)imageName number:(int)number
{
    if (_catImgView.isAnimating == YES)
    {
        return;
    }
    NSMutableArray *imgArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < number; i++)
    {
        NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg",imageName,i];
        UIImage *image = [UIImage imageNamed:name];
        [imgArray addObject:image];
    }
    _catImgView.animationDuration = 0.1 * imgArray.count;
    _catImgView.animationImages = imgArray;
    _catImgView.animationRepeatCount = 1;
    [_catImgView startAnimating];
}

//点击按钮,创建图片数组,实现三个属性、一个方法
- (void)animationClick:(UIButton *)button
{
    //方法1
    NSString *name = [_btnImgNameArray objectAtIndex:button.tag];
    NSString *numStr =[_numberArry objectAtIndex:button.tag];
    int num = [numStr intValue];
    [self tomAnimationWithImageName:name number:num];
}

1.****屏幕宽高,按钮宽高
2.

**x = colSpace + i * ****(****colSpace + width****)**
**y = rowSpace + j * ****(****rowSpace + height****)**

1.用定时器,制作一个椭圆运动效果
a.中点坐标
b.根据半径求园坐标
宏定义:中点坐标x/y,半径,

2.雪花下落,多片

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容