随手整理
1.取得2個文本輸入框的值
NSString *text1 = self.num1.text;
NSString *text2 = self.num2.text;
2.退出鍵盤
// 第一響應者:叫出鍵盤的那個控件
// 不當第一響應者(就會把鍵盤退下)
[self.num1 resignFirstResponder];
[self.view endEditing:YES];//退出鍵盤
// 不允許直接修改 ?對象的 結構體屬性的成員
// 允許直接對象的結構體屬性
? ??
3.設置按鈕的背景圖,
UIImage *image = [UIImage imageNamed:@"btn_01"];//根據圖片名創建一張圖片
[btn setBackgroundImage:image forState:uicontrolstate];//設置按鈕在背景圖片
4.執行動畫
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];//動畫執行的時間
中間部分為要執行的動畫代碼
[UIView commitAnimations];
? ?
5.手動創建按鈕以及設置按鈕的一些基本屬性
UIButton *btn = [[UIButton alloc] init];//創建按鈕
[self.view addSubview:btn];//把創建的按鈕添加到父控件當中
btn.frame = CGRectMake(50, 50, 100, 100);//設置按鈕的位置以及大小
UIImage *normal = [UIImage imageNamed:@"btn_01"];//通過文件名加載圖片(凡是PNG圖片,都不用加拓展名)
[btn setBackgroundImage:normal forState:UIControlStateNormal];//設置普通狀態下的背景圖片
UIImage *high = [UIImage imageNamed:@"btn_02"];
[btn setBackgroundImage:high forState:UIControlStateHighlighted];//加載高亮的圖片
[btn setTitle:@"點我啊" forState:UIControlStateNormal];//設置按鈕在普通狀態下的文字
[btn setTitle:@"摸我干啥" forState:UIControlStateHighlighted];//設置按鈕在高亮狀態下的文字
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//設置按鈕在普通狀態下的文字的顏色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];//設置按鈕在高亮狀態下的文字顏色
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];監聽按鈕點擊,當按鈕點擊時調用btnClick:方法 ??
btn2.center = CGPointMake(250, 250);//設置按鈕的中點位置
6 transform屬性
btn.transform = CGAffineTransformMakeRotation(-M_PI_4);//設置按鈕的旋轉屬性,但不是在上一次的基礎上進行旋轉的,一直是相對于它最初的位置來講的
btn.transform = CGAffineTransformRotate(head.transform, M_PI_4);//設置按鈕在原來的基礎上進行旋轉45度,角度是正數:順時針, 角度是負數:逆時針
head.transform = CGAffineTransformMakeTranslation(0, -100);//平移屬性,,不是在上一次的基礎上進行平移,一直是相對于它最初的位置來講的
head.transform = CGAffineTransformTranslate(head.transform, 0, -100);在上一次的基礎上進行平移
head.transform = CGAffineTransformMakeScale(1.5, 1.5);//縮放屬性,1.5為縮放的比例,縮放比例小于1,是為縮小,大于1是為放大不是在上一次的基礎上進行縮,一直是相對于它最初的位置來講的
head.transform = CGAffineTransformScale(head.transform, 1.5, 1.5);在上一次的基礎上進行縮放 ?
UIButton *head = (UIButton *)[self.view viewWithTag:10];//獲也Tag為10的那個按鈕
7.imageView序列幀動畫
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
? ? if (self.tom.isAnimating) return;//如果動畫正在播放,不允再繼續播放
? ? NSMutableArray *images = [NSMutableArray array];//創建一個可變數組,存放所有的圖
? ? for (int i = 0; i<count; i++) {
? ? ? ? NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];//根據傳入的參數拼接圖片名
? ? ? ? NSBundle *bundle = [NSBundle mainBundle];//獲取應用程序的資源路徑
? ? ? ? NSString *path = [bundle pathForResource:filename ofType:nil];//根據文件名獲取該文件在應用程序中的資源路徑
? ? ? ? UIImage *image = [UIImage imageWithContentsOfFile:path];//根據圖片的路徑獲取一張圖片
? ? ? ? [images addObject:image];//把圖片添加到可變數組中
? ? }
? ? self.tom.animationImages = images;//按順序播放數組images中的圖片
? ? self.tom.animationRepeatCount = 1;//調置播放的次數
? ? self.tom.animationDuration = images.count * 0.05;//設置播放的時間
? ? [self.tom startAnimating];//開始播放動畫
? ? CGFloat delay = self.tom.animationDuration + 1.0;//播放完畢一秒后清除內存
? ? [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}
注意: ??
用UIImage *image = [UIImage imageNamed:filename];獲取圖片會有緩存
UIImage *image = [UIImage imageWithContentsOfFile:path];//根據圖片的路徑獲取一張圖片,用這種方法獲取的圖片沒有緩存
8.九宮格算法
? ? int totalColumns = 3;//總列數(一行最多3列)
? ? CGFloat appW = 85;//九宮格內部控件的寬度
? ? CGFloat appH = 90;//九宮格內部控件的高度
? ? 間隙 = (控制器view的寬度 - 總列數 * 內部控件寬度) / 總列數+1
? ? CGFloat marginX = (self.view.frame.size.width - totalColumns * appW) / (totalColumns + 1);
? ? CGFloat marginY = 15;
? ? for (int index = 0; index<self.apps.count; index++) {
? ? ? ? UIView *appView = [[UIView alloc] init];
? ? ? ? appView.backgroundColor = [UIColor redColor];
? ? ? ? int row = index / totalColumns;//計算當前所在的行
? ? ? ? int col = index % totalColumns;//計算當前所在的列
? ? ? ? CGFloat appX = marginX + col * (appW + marginX);//計算當前控件的x值 (間隔+當前所在的列*(控件的寬度+一個間隔));
? ? ? ? CGFloat appY = 30 + row * (appH + marginY);//計算當前控件的y值 (第一個控件Y的值 + 當前所在的行* (控件的高度 + 一個間隔));
? ? ? ? appView.frame = CGRectMake(appX, appY, appW, appH);//設置內部控件的位置和大小
? ? ? ? [self.view addSubview:appView];//把創建的控件添加到控制器View當中
? ? ? ?}
9.通過xib創建局部控件,利用模型封裝加載數據
? ? 1.創建一個模型XQApp來封裝從plist文件文件中讀取的數據.模型中要提供兩個方法來來初始化模型
? ? - (instancetype)initWithDict:(NSDictionary *)dict;
? ? + (instancetype)appWithDict:(NSDictionary *)dict;
? ? - (instancetype)initWithDict:(NSDictionary *)dict
? ? ? ?{
? ? ? ? if (self = [super init]) {
? ? ? ? self.name = dict[@"name"];
? ? ? ? self.icon = dict[@"icon"];
? ? ? ?}
? ? ? ?return self;
? ? ? ?} ?
? ? + (instancetype)appWithDict:(NSDictionary *)dict//根據字典加載模型
? ? {
? ? ? ?return [[self alloc] initWithDict:dict];
? ? }
? ? 2.在控制器當中定義一個NSArray數組*apps,用來存放每一個封裝好了的模型.采用懶加載方式,重寫get方法
? ? - (NSArray *)apps
? ? {
? ? ? ? if (_apps == nil) {//判斷數組中是否為空, 如果為空的放話,那么從plist文件加載數據.
? ? ? ? ? ? NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];//獲取app.plist文件的資源路徑
? ? ? ? ? ? NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];//從獲取的路徑中加載數組
? ? ? ? ? ? NSMutableArray *tempAppArray = [NSMutableArray array];//定義一個臨時存放每一個模型的可加變數組!
? ? ? ? ? ? for (NSDictionary *dict in dictArray) {//遍歷從plist文件中讀取的數組
? ? ? ? ? ? ? ? MJApp *app = [MJApp appWithDict:dict];//將數組中取出的每一個字典放傳入到模型的初始化方法中, 給模型進行賦值,然后返回這個模型
? ? ? ? ? ? ? ? [tempAppArray addObject:app];把返回的模型放到臨時數組當中
? ? ? ? ? ? }
? ? ? ? ? ? _apps = appArray;//把臨時數中賦值給控制器定義的,app數組中.
? ? }
? ? return _apps;
? ? }
? ? 3.創建一個名為MJAppView.xib的Xib文件,在xib文件中拖放一個View,再往view中拖入其它的子控件.
? ? 4.創建一個繼承UIView類, 名為MJAppView,把xib的Custom Class中的class改為MJAppView
? ? 5.再把xib中的子控件拖線連到MJAppView中,做為MJAppView的屬性.
? ? 6.在MJAppView頭文件定義一個MJApp *app;履性,然后重寫app的set方法,給內總屬性進行賦值
? ? - (void)setApp:(MJApp *)app
? ? {
? ? ? ? _app = app;
? ? ? ? self.iconView.image = [UIImage imageNamed:app.icon];
? ? ? ? self.nameLabel.text = app.name;
? ? }
? ? 用來存入傳入過來的模型數據.再提供一個類方法用來加載xib文件,并傳入一個模型,給MJAppView中的app,屬性時賦值,
? ? + (instancetype)appViewWithApp:(MJApp *)app;
? ? + (instancetype)appViewWithApp:(MJApp *)app
? ? {
? ? ? ? NSBundle *bundle = [NSBundle mainBundle];
? ? ? ? NSArray *objs = [bundle loadNibNamed:@"MJAppView" owner:nil options:nil];//讀取xib文件(會創建xib中的描述的所有對象,并且按順序放到數組中返回)
? ? ? ? MJAppView *appView = [objs lastObject];//獲得返回xib對象數組中的最后一個元素
? ? ? ? appView.app = app;//給MJAppView中的app屬性進行賦值,就會調用上面的set方法
? ? ? ? return appView;//返回加載好的對象
? ? }
? ? 7.在控件器當中創建給MJAppView對象
? ? MJAppView *appView = [MJAppView appViewWithApp:self.apps[index]];//index為控制器中app對象數組中的下標
? ?[self.view addSubview:appView];
10設置狀態欄的樣式為白色
- (UIStatusBarStyle)preferredStatusBarStyle
{
? ? return UIStatusBarStyleLightContent;//狀態欄的樣式為白色
}
11.設置按鈕的狀態
self.nextQuestionBtn.enabled = self.index != (self.questions.count - 1);//設置按鈕狀態為可用或不可用
optionBtn.hidden = YES;//設置按鈕隱藏
12.讓子控件一起執行同一個方法.
[self.optionView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
13.延遲0.5秒執行selector()中的方法
[self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];
14.將一個UIView顯示到最前面只需要調用父類的bringSubviewToFront
[self.view bringSubviewToFront:self.iconBtn];
15.將一個UIView顯示到最后面只需要調用父類的sendSubviewToBack
[self.view sendSubviewToBack:self.iconBtn];
NSString *text1 = self.num1.text;
NSString *text2 = self.num2.text;
2.退出鍵盤
// 第一響應者:叫出鍵盤的那個控件
// 不當第一響應者(就會把鍵盤退下)
[self.num1 resignFirstResponder];
[self.view endEditing:YES];//退出鍵盤
// 不允許直接修改 ?對象的 結構體屬性的成員
// 允許直接對象的結構體屬性
? ??
3.設置按鈕的背景圖,
UIImage *image = [UIImage imageNamed:@"btn_01"];//根據圖片名創建一張圖片
[btn setBackgroundImage:image forState:uicontrolstate];//設置按鈕在背景圖片
4.執行動畫
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];//動畫執行的時間
中間部分為要執行的動畫代碼
[UIView commitAnimations];
? ?
5.手動創建按鈕以及設置按鈕的一些基本屬性
UIButton *btn = [[UIButton alloc] init];//創建按鈕
[self.view addSubview:btn];//把創建的按鈕添加到父控件當中
btn.frame = CGRectMake(50, 50, 100, 100);//設置按鈕的位置以及大小
UIImage *normal = [UIImage imageNamed:@"btn_01"];//通過文件名加載圖片(凡是PNG圖片,都不用加拓展名)
[btn setBackgroundImage:normal forState:UIControlStateNormal];//設置普通狀態下的背景圖片
UIImage *high = [UIImage imageNamed:@"btn_02"];
[btn setBackgroundImage:high forState:UIControlStateHighlighted];//加載高亮的圖片
[btn setTitle:@"點我啊" forState:UIControlStateNormal];//設置按鈕在普通狀態下的文字
[btn setTitle:@"摸我干啥" forState:UIControlStateHighlighted];//設置按鈕在高亮狀態下的文字
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//設置按鈕在普通狀態下的文字的顏色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];//設置按鈕在高亮狀態下的文字顏色
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];監聽按鈕點擊,當按鈕點擊時調用btnClick:方法 ??
btn2.center = CGPointMake(250, 250);//設置按鈕的中點位置
6 transform屬性
btn.transform = CGAffineTransformMakeRotation(-M_PI_4);//設置按鈕的旋轉屬性,但不是在上一次的基礎上進行旋轉的,一直是相對于它最初的位置來講的
btn.transform = CGAffineTransformRotate(head.transform, M_PI_4);//設置按鈕在原來的基礎上進行旋轉45度,角度是正數:順時針, 角度是負數:逆時針
head.transform = CGAffineTransformMakeTranslation(0, -100);//平移屬性,,不是在上一次的基礎上進行平移,一直是相對于它最初的位置來講的
head.transform = CGAffineTransformTranslate(head.transform, 0, -100);在上一次的基礎上進行平移
head.transform = CGAffineTransformMakeScale(1.5, 1.5);//縮放屬性,1.5為縮放的比例,縮放比例小于1,是為縮小,大于1是為放大不是在上一次的基礎上進行縮,一直是相對于它最初的位置來講的
head.transform = CGAffineTransformScale(head.transform, 1.5, 1.5);在上一次的基礎上進行縮放 ?
UIButton *head = (UIButton *)[self.view viewWithTag:10];//獲也Tag為10的那個按鈕
7.imageView序列幀動畫
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
? ? if (self.tom.isAnimating) return;//如果動畫正在播放,不允再繼續播放
? ? NSMutableArray *images = [NSMutableArray array];//創建一個可變數組,存放所有的圖
? ? for (int i = 0; i<count; i++) {
? ? ? ? NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];//根據傳入的參數拼接圖片名
? ? ? ? NSBundle *bundle = [NSBundle mainBundle];//獲取應用程序的資源路徑
? ? ? ? NSString *path = [bundle pathForResource:filename ofType:nil];//根據文件名獲取該文件在應用程序中的資源路徑
? ? ? ? UIImage *image = [UIImage imageWithContentsOfFile:path];//根據圖片的路徑獲取一張圖片
? ? ? ? [images addObject:image];//把圖片添加到可變數組中
? ? }
? ? self.tom.animationImages = images;//按順序播放數組images中的圖片
? ? self.tom.animationRepeatCount = 1;//調置播放的次數
? ? self.tom.animationDuration = images.count * 0.05;//設置播放的時間
? ? [self.tom startAnimating];//開始播放動畫
? ? CGFloat delay = self.tom.animationDuration + 1.0;//播放完畢一秒后清除內存
? ? [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}
注意: ??
用UIImage *image = [UIImage imageNamed:filename];獲取圖片會有緩存
UIImage *image = [UIImage imageWithContentsOfFile:path];//根據圖片的路徑獲取一張圖片,用這種方法獲取的圖片沒有緩存
8.九宮格算法
? ? int totalColumns = 3;//總列數(一行最多3列)
? ? CGFloat appW = 85;//九宮格內部控件的寬度
? ? CGFloat appH = 90;//九宮格內部控件的高度
? ? 間隙 = (控制器view的寬度 - 總列數 * 內部控件寬度) / 總列數+1
? ? CGFloat marginX = (self.view.frame.size.width - totalColumns * appW) / (totalColumns + 1);
? ? CGFloat marginY = 15;
? ? for (int index = 0; index<self.apps.count; index++) {
? ? ? ? UIView *appView = [[UIView alloc] init];
? ? ? ? appView.backgroundColor = [UIColor redColor];
? ? ? ? int row = index / totalColumns;//計算當前所在的行
? ? ? ? int col = index % totalColumns;//計算當前所在的列
? ? ? ? CGFloat appX = marginX + col * (appW + marginX);//計算當前控件的x值 (間隔+當前所在的列*(控件的寬度+一個間隔));
? ? ? ? CGFloat appY = 30 + row * (appH + marginY);//計算當前控件的y值 (第一個控件Y的值 + 當前所在的行* (控件的高度 + 一個間隔));
? ? ? ? appView.frame = CGRectMake(appX, appY, appW, appH);//設置內部控件的位置和大小
? ? ? ? [self.view addSubview:appView];//把創建的控件添加到控制器View當中
? ? ? ?}
9.通過xib創建局部控件,利用模型封裝加載數據
? ? 1.創建一個模型XQApp來封裝從plist文件文件中讀取的數據.模型中要提供兩個方法來來初始化模型
? ? - (instancetype)initWithDict:(NSDictionary *)dict;
? ? + (instancetype)appWithDict:(NSDictionary *)dict;
? ? - (instancetype)initWithDict:(NSDictionary *)dict
? ? ? ?{
? ? ? ? if (self = [super init]) {
? ? ? ? self.name = dict[@"name"];
? ? ? ? self.icon = dict[@"icon"];
? ? ? ?}
? ? ? ?return self;
? ? ? ?} ?
? ? + (instancetype)appWithDict:(NSDictionary *)dict//根據字典加載模型
? ? {
? ? ? ?return [[self alloc] initWithDict:dict];
? ? }
? ? 2.在控制器當中定義一個NSArray數組*apps,用來存放每一個封裝好了的模型.采用懶加載方式,重寫get方法
? ? - (NSArray *)apps
? ? {
? ? ? ? if (_apps == nil) {//判斷數組中是否為空, 如果為空的放話,那么從plist文件加載數據.
? ? ? ? ? ? NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];//獲取app.plist文件的資源路徑
? ? ? ? ? ? NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];//從獲取的路徑中加載數組
? ? ? ? ? ? NSMutableArray *tempAppArray = [NSMutableArray array];//定義一個臨時存放每一個模型的可加變數組!
? ? ? ? ? ? for (NSDictionary *dict in dictArray) {//遍歷從plist文件中讀取的數組
? ? ? ? ? ? ? ? MJApp *app = [MJApp appWithDict:dict];//將數組中取出的每一個字典放傳入到模型的初始化方法中, 給模型進行賦值,然后返回這個模型
? ? ? ? ? ? ? ? [tempAppArray addObject:app];把返回的模型放到臨時數組當中
? ? ? ? ? ? }
? ? ? ? ? ? _apps = appArray;//把臨時數中賦值給控制器定義的,app數組中.
? ? }
? ? return _apps;
? ? }
? ? 3.創建一個名為MJAppView.xib的Xib文件,在xib文件中拖放一個View,再往view中拖入其它的子控件.
? ? 4.創建一個繼承UIView類, 名為MJAppView,把xib的Custom Class中的class改為MJAppView
? ? 5.再把xib中的子控件拖線連到MJAppView中,做為MJAppView的屬性.
? ? 6.在MJAppView頭文件定義一個MJApp *app;履性,然后重寫app的set方法,給內總屬性進行賦值
? ? - (void)setApp:(MJApp *)app
? ? {
? ? ? ? _app = app;
? ? ? ? self.iconView.image = [UIImage imageNamed:app.icon];
? ? ? ? self.nameLabel.text = app.name;
? ? }
? ? 用來存入傳入過來的模型數據.再提供一個類方法用來加載xib文件,并傳入一個模型,給MJAppView中的app,屬性時賦值,
? ? + (instancetype)appViewWithApp:(MJApp *)app;
? ? + (instancetype)appViewWithApp:(MJApp *)app
? ? {
? ? ? ? NSBundle *bundle = [NSBundle mainBundle];
? ? ? ? NSArray *objs = [bundle loadNibNamed:@"MJAppView" owner:nil options:nil];//讀取xib文件(會創建xib中的描述的所有對象,并且按順序放到數組中返回)
? ? ? ? MJAppView *appView = [objs lastObject];//獲得返回xib對象數組中的最后一個元素
? ? ? ? appView.app = app;//給MJAppView中的app屬性進行賦值,就會調用上面的set方法
? ? ? ? return appView;//返回加載好的對象
? ? }
? ? 7.在控件器當中創建給MJAppView對象
? ? MJAppView *appView = [MJAppView appViewWithApp:self.apps[index]];//index為控制器中app對象數組中的下標
? ?[self.view addSubview:appView];
10設置狀態欄的樣式為白色
- (UIStatusBarStyle)preferredStatusBarStyle
{
? ? return UIStatusBarStyleLightContent;//狀態欄的樣式為白色
}
11.設置按鈕的狀態
self.nextQuestionBtn.enabled = self.index != (self.questions.count - 1);//設置按鈕狀態為可用或不可用
optionBtn.hidden = YES;//設置按鈕隱藏
12.讓子控件一起執行同一個方法.
[self.optionView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
13.延遲0.5秒執行selector()中的方法
[self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];
14.將一個UIView顯示到最前面只需要調用父類的bringSubviewToFront
[self.view bringSubviewToFront:self.iconBtn];
15.將一個UIView顯示到最后面只需要調用父類的sendSubviewToBack
[self.view sendSubviewToBack:self.iconBtn];
總結
- 上一篇: 在日常生活中,简易合同的重要性 | 每天
- 下一篇: 202302|读书笔记——国图点滴