一、九宮格
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.雪花下落,多片