1. 熟悉Git的基本流程
- git clone
- git add -A
- git commit -m " "
- git push
- git pull
2. OC(Objective - C)中的類和對象
- 熟悉使用編程過程中常用的快捷鍵 并熟練使用 提高代碼速度.
- 類和對象
- 類: 一個具有相同特征和行為的事物的抽象集合.
- 對象: 類的實例也成為類的實現.
- OOP(面向對象編程) OOD(面向對象設計)
- 面向對象的三大特性: 封裝 繼承 多態 (都是為了提高代碼的復用)
- 創建一個Person類 包含name,age和sex三個實例變量
{
@public
NSString *_name;
@private
NSInteger _age;
@protected
NSString *_sex;
}
@public (公有的, 本類,子類和其他類都可以使用)
@protected (受保護的,本類和子類可以使用)
@private (私有的, 只有本類才可以使用)
- (void)setName:(NSString *)name; // setter方法(賦值方法) - (NSString *)name; // getter方法(取值方法) - (void)sleep:(NSInteger)time; // 實例方法的聲明: 返回值類型 + 方法名 + 參數列表
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age; // 初始化方法
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age; // 遍歷構造器
3.字符串和數值
-
NSString 的方法
/** 初始化(init) /
NSString str = [[NSString alloc] init];
/ 便利構造器(string) /
NSString str1 = [NSString string];
/ 獲取字符串長度(length) /
NSUInteger length = str.length;
/* 獲取字符串中字符(character) /
/* 字符串是否相等(equal) /
[str isEqualToString:str1];
/* 獲取子串(sub) /
/* 拼接字符串(append) */
[str stringByAppendingString:@"dsdsddf"];
[str stringByAppendingFormat:@"kfkdjf%@", str];NSString *pathStr = @"/users/local/bin"; // 有返回值的需要接收一下 pathStr = [pathStr stringByAppendingPathComponent:@"name.plist"]; NSLog(@"%@", pathStr); /** 替換字符串(replace) */ // str stringByReplacingCharactersInRange:<#(NSRange)#> withString:<#(nonnull NSString *)#> /** 字符串對象轉化為int類型(int) */ [str intValue]; /** 字符串對象轉化為其他數據類型, 例如double, float, BOOL等. */ [str doubleValue]; [str floatValue]; [str boolValue]; /** 字符串全部大寫(uppercase) */ [str uppercaseString]; /** 字符串全部小寫(lowercase) */ [str lowercaseString]; /** 字符串首字母大寫(capitalized) */ [str capitalizedString]; /** 是否以指定字符串為前綴(prefix) */ BOOL a = [str hasPrefix:@"sd"]; /** 是否以指定字符串為后綴(suffix) */ BOOL b = [str hasSuffix:@"dsd"];
-
數組
數組: 分為可變數組 NSMutableArray 與 不可變數組 NSArray
數組中只能存放對象類型元素
數組主要是有序存放元素的集合類型 并且可以通過下標取值
不可變數組只能取值 可變數組可以増 刪 改
2.1 初始化一個不可變數組
1> initWithObjects + 任意對象類型
有序填入每個元素 初始化結束以nil為判別標準 遇到nil 就表示數組結束
當數組里存在空元素時 如果用初始化 或者 構造器的話 程序運行不會報錯 我們可能會不在意 可是此時數組中元素已經丟失 而字面量方式寫的 數組可以使程序crash 能很直接的告訴我們 數組中存在空值 所以一般在創建數組時用字面量方法 我們也能知道 在數組中元素不能有空值
NSArray *array = [[NSArray alloc] initWithObjects: @"123", @"234", @"345", @"456", nil];
2> 構造器方法
NSArray *arr = [NSArray arrayWithObjects:@"123", @"234", @"345", @"456", nil];
3> 字面量 同 NSString @"" NSNumber @()
NSArray *myArray = @[@"123", @"234", @"345", @"456"];
2.2 數組的取值
1> count 取數組元素的個數
NSLog(@"%ld", array.count);
2> 通過下標取出數組元素
NSString *numStr = [array objectAtIndex:2];
3> array[0] 語法糖 取值
NSString *numStr1 = array[0];
4> 根據值取下標
NSUInteger indexObject = [array indexOfObject:@"234"];
NSLog(@"%ld", indexObject);
5> 用下面的方式來取數組中的元素 可以避免數組為空時 不必要的crash
[array lastObject];
[array firstObject];
2.3 初始化可變數組(不推薦使用字面量)
NSMutableArray *array = [NSMutableArray array];
1> 數組中添加元素
[array addObject:@"777"];
2> 插入
[array insertObject:@"hahah" atIndex:1];
3> 移除
[array removeObject:@"777"];
[array removeAllObjects];
4> 替換
[array replaceObjectAtIndex:0 withObject:@"zhaohao"];可變數組和不可變數組的區別
- 可變指的是本身可變 不可變要是想改變的話 就要用一個數組來接收
- 可變 繼承于不可變 也就是說可變的數組是不可變的子類
字典
存儲鍵值對(key - value)的集合類型
字典存儲數據是無序的
通過key值存儲 查找value
1> 初始化
先value 后key
字典中只能存放對象類型
如果有數字 將數字轉化為NSNumber類型
3.1 不可變字典
1> 不可變字典初始化
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"tantan", @"name", [NSNumber numberWithInt:18], @"age", @"tantan", @"firstName", nil];
2> 不可變字典遍歷構造器
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"jiajia", @"name", [NSNumber numberWithInt:20], @"age", nil];
3> 字面量 先 key 后value (數字為NSNumber類型)
NSDictionary *dic2 = @{@"name": @"zhengyu", @"age": @(20), @"sex": @"man"};
4> 取值 通過key值取對應的value
NSString *str = [dic2 objectForKey:@"name"];
NSLog(@"%@", str);
5> 獲取所有key值
NSArray *keyArr = [dic2 allKeys];
NSLog(@"%@", keyArr);
6> 獲取所有value
NSArray *valueArr = [dic2 allValues];
NSLog(@"%@", valueArr);
7> count 與key 或 value的個數相同
NSUInteger count = [dic2 count];
NSLog(@"%ld", count);
8> 通過一個字典的key 取另一個字典中對應value數組
NSArray *allValueArr = [dic objectsForKeys:dic2.allKeys notFoundMarker:[NSNumber class]];
NSLog(@"%@", allValueArr);
9> 有可能兩個相同的value 對應的key不同 用下面的方法就可以查找出所有不同的key 將這些key 放到一個數組里
NSArray *keyArray = [dic allKeysForObject:@"tantan"];
NSLog(@"%@", keyArray);
10> dic[@"name"]; (語法糖) dic[key]
NSLog(@"%@", dic1[@"name"]);
3.2 可變字典
1> 可變字典初始化方式 (不推薦使用字面量)
NSMutableDictionary *mutDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"yuhan", @"name", @"man", @"sex", @"18", @"age", nil];
NSMutableDictionary *mutDic1 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *mutDic = [@{} mutableCopy]; (不推薦)
2> 取值與不可變 相同
3> 可變字典可以進行増 刪 改 操作
4>. 給字典添加鍵值對 用setObject: forKey: 如果新增加的key存在 會將之前的value覆蓋 如果key不存在 會使字典增加一個鍵值對1
[mutDic setObject:@"yuhan" forKey:@"name"];
[mutDic1 setObject:@"man" forKey:@"sex"];
NSLog(@"%@, %@", mutDic, mutDic1);
5>. 刪除字典中的鍵值對
[mutDic removeObjectForKey:@"name"];
NSLog(@"%@", mutDic);
[mutDic removeAllObjects];
NSLog(@"%@", mutDic);
6>. 語法糖 賦值 與 字典添加鍵值對作用一樣 可以向字典里添加鍵值對 也可以從字典里取 值
mutDic[@"where"] = @"huludao";
mutDic[@"birth"] = @"1993";
mutDic[@"study"] = @"college";
NSLog(@"%@", mutDic);
NSLog(@"%@", mutDic[@"where"]);遍歷和排序
4.1 遍歷
4.1.1 for循環遍歷數組
將數組倒序輸出 存放在一個可變數組中
首先創建一個不可變數組 讓后用可變數組接收
NSArray *arr = @[@"偉仔", @"阿美", @"飛哥", @"糖糖", @"瑞瑞"];
NSMutableArray *mutArr = [NSMutableArray array];
for (NSInteger i = arr.count - 1; i >= 0; i--) {
[mutArr addObject:arr[i]];
}
NSLog(@"%@", mutArr);
4.1.2 for循環遍歷字典
定義一個字典 遍歷輸出 name sex age
NSDictionary *dic = @{@"name": @"陳冠希", @"sex": @"man", @"age": @"35"};
NSArray *keyArr = dic.allKeys;
for (NSInteger i = 0; i < keyArr.count; i++) {
NSString *key = keyArr[i];
NSString *value = [dic objectForKey:key];
NSLog(@"%@ = %@", key, value);
4.1.3 枚舉器遍歷數組
NSEnumerator *stringArrayEnumerator = [arr objectEnumerator];
id value = nil;
while ((value = [stringArrayEnumerator nextObject])) {
NSLog(@"%@", value);
}
// 倒序輸出
NSEnumerator *rever = [arr reverseObjectEnumerator];
id value1 = nil;
while ((value1 = [rever nextObject])) {
NSLog(@"%@", value1);
}
4.1.4 枚舉器遍歷字典
字典 枚舉器 默認打印字典的value 也可以打印所有的key值 無序的
NSEnumerator *dicEnumerator = [dic.allKeys objectEnumerator];
id value2 = nil;
while (value2 = [dicEnumerator nextObject]) {
// NSArray *allKey = [dic allKeysForObject:value2]; 取key
NSLog(@"%@", value2);
}
4.1.5 枚舉器遍歷集合
NSSet *set = [NSSet setWithObjects:@"q", @"w", @"e", @"r", nil];
NSEnumerator *setEnu = [set objectEnumerator];
id setValue = nil;
while (setValue = [setEnu nextObject]) {
NSLog(@"%@", setValue);
}
4.1.6 快速枚舉(for...in...) 遍歷數組
NSString *newStr = [NSString string];
for (NSString *string in arr) {
NSLog(@"%@", string);
newStr = [newStr stringByAppendingString:string];
}
NSLog(@"%@", newStr);
4.1.7 快速枚舉遍歷字典
for (NSString *dicStr in dic) {
NSLog(@"%@", dicStr);
NSLog(@"%@", [dic objectForKey:dicStr]);
}
4.1.8 集合 快速遍歷
for (NSString *setStr in set) {
NSLog(@"%@", setStr);
}
4.2 排序
參數1 key: 排序條件 參數2 acending YES(升序) / NO(降序)
數組有排序的方法
NSArray *numArr = @[@1, @7, @5, @4];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
numArr = [numArr sortedArrayUsingDescriptors:@[sort]];
NSLog(@"num: %@", numArr);
可變數組與不可變數組 排序使用的方法不同
NSMutableArray *numMutArr = [NSMutableArray arrayWithObjects:@"1", @"5", @"6", @"2", nil];
NSSortDescriptor *mutSort = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
[numMutArr sortUsingDescriptors:@[mutSort]];
NSLog(@"%@", numMutArr);
4. 內存管理
- 內存溢出 沒存用盡 導致crash
- 野指針 指針指向被釋放的地方 過度釋放
內存方式
垃圾回收機制 只有mac有
MRC 手動管理引用計數
ARC 自動管理引用計數 ARC基于MRC
看到 retain alloc copy 引用計數加1 需要進行內存管理
NSObject *objc = [[NSObject alloc] init];
[objc retain];
// 需要兩次釋放
[objc release];
[objc release];
release autorelease 會對引用計數減1
當對象的引用計數為0時 系統會調用相應的dealloc方法
ARC
語義特性: strong(強引用 引用計數加1) weak(應用于簡單對象類型 弱引用 對象被釋放時 對象會自動置空) retain(強引用 引用計數加1) copy(吧對象拷貝一份 原來的引用計數不變 新對象的引用計數加1) assign(應用于簡單數據類型 不會產生引用計數)
協議 - 聲明協議
- 簽訂協議
- 實現協議方法
注意事項:
在ARC工程下使用MRC的文件(三方) -fno-objc-arc
在MRC工程下使用ARC的文件 -fobjc-arc
需要加在 build phases下的編譯source下
5. 類的擴展
- 類目
category(類別 類目 分類)
創建category (通過Object - C創建會產生兩個文件 文件名: 類 + category名 .h / .m)
例: 創建一個類目, 給NSString添加方法 獲取字符串首字母并將其大寫
.h文件
#import <Foundation/Foundation.h>
@interface NSString (Uper)
- (void)getStr:(NSString *)str;
- (NSString *)getFirstChar;
@end
.m文件
#import "NSString+Uper.h"
@implementation NSString (Uper)
- (void)getStr:(NSString *)str {
NSString *str1 = [str uppercaseString];
NSLog(@"%@", [str1 substringToIndex:1]);
}
- (NSString *)getFirstChar {
NSString *first = [self substringWithRange:NSMakeRange(0, 1)];
return first.uppercaseString;
}
@end
main.m
#import <Foundation/Foundation.h>
#import "NSString+Uper.h"
int main(int argc, const char * argv[]) {
// 獲取字符串首字母 并將其大寫
NSString *str = @"zhaohao";
[str getStr:str];
NSString *first = [str getFirstChar];
NSLog(@"%@", first);
return 0;
}
-
延展(Extension) 一般定義在.m文件里
例:
.h文件中聲明
#import <Foundation/Foundation.h>@interface Lovers : NSObject - (void)getLoverLife; @end
.m文件
#import "Lovers.h"
// 延展的聲明
@interface Lovers ()
// 使用延展 可以保證.h的簡潔 外界要用到的屬性 / 方法才在.h中聲明
// 延展中 主要實現屬性 / 方法
@property (nonatomic, copy) NSString *cloth;
// 聲明方法
- (void)shopping;
@end
@implementation Lovers
- (void)shopping {
NSLog(@"lalala");
}
- (void)whatchTV {
NSLog(@"HAHAH");
}
- (void)doSport {
NSLog(@"HEIHEIHEI");
}
- (void)getLoverLife {
[self shopping];
[self doSport];
[self doSport];
}
@end
-
代理delegate模式
Student.h
#import <Foundation/Foundation.h>
// 第一步 規定了協議內容
@protocol GetMacBook <NSObject>
- (NSString *)buyMacBook:(NSString *)money; // 默認必須實 現
@optional // 可選的實現方法
- (void)drawBill;
@end@interface Student : NSObject // 第二步 設定委托方 // 委托方的寫法 @property (nonatomic, weak) id <GetMacBook> delegate; // 什么時候觸發代理 - (void)seeWeChat; @end
Student.m
#import "Student.h"
@implementation Student
// 第三步 讓代理人執行方法
- (void)seeWeChat {
// 代理人可能沒有實現協議方法
// 判斷代理人是否存在 判斷代理人能否響應 對應的協議方法 當兩者同時滿足 執行
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(buyMacBook:)]) {
NSString *mac = [self.delegate buyMacBook:@"14000"];
NSLog(@"%@", mac);
}
}
@end
BuyPerson.h
#import <Foundation/Foundation.h>
#import "Student.h"
// 第四步 簽訂協議
@interface BuyPerson : NSObject <GetMacBook> // 簽協議
@end
BuyPerson.m
#import "BuyPerson.h"
@implementation BuyPerson
// 實現協議方法
- (NSString *)buyMacBook:(NSString *)money {
NSLog(@"%@", money);
return @"mac";
}
@end
main.m
Student *stu = [[Student alloc] init];
BuyPerson *per = [[BuyPerson alloc] init];
// 設定代理人
stu.delegate = per;
[stu seeWeChat]
6. iOS新特性
-
nonnull nullable null_resettable修飾屬性
Person.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN@interface Persson : NSObject // Nullability(判斷為不為空的能力) 是在編譯層次上加了一些改 動 (只能作用于對象類型) // nonnull聲明的屬性不能為空 (setter和getter方法) // @property (nonatomic, nonnull, assign) NSInteger age; (錯的) @property (nonatomic, nonnull, copy) NSString *name; @property (nonatomic, copy) NSString * __nonnull size; // nullable聲明屬性可以為空 (setter和getter方法) @property (nonatomic, nullable, copy) NSString *sex; @property (nonatomic, copy) NSString * __nullable study; // UIViewController中的view屬性 @property (null_resettable, nonatomic, strong) NSArray *friends; @property (nonatomic, strong) NSDictionary *dic; // NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END // 一般應用于不頭文件.h 一般將屬性包含起來 默認 nonnull 如 果是其他修飾要加 例如 nullable null_resettable 要寫 NS_ASSUME_NONNULL_END @end
Person.m
#import "Persson.h"
@implementation Persson
// null_resettable 聲明屬性時會阿生沖突 需要重寫setter方法處理掉為空的情況
- (void)setFriends:(NSArray *)friends {
if (friends == nil) {
friends = [NSArray array];
return;
}
_friends = friends;
}
@end
main.m
Persson *per1 = [[Persson alloc] init];
// nonnull 修飾屬性 對應的getter和setter方法
per1.name = nil; // 不能為空
per1.name = @"zhoahao";
per1.size = nil; // 不能為空
// nullable 修飾屬性 對應的getter和setter方法
// [per1 setSex:<#(NSString * _Nullable)#>];
per1.study = nil;
// null_resettable
// [per1 setFriends:<#(NSArray * _Nullable)#>] set 方法可以為空值
// [per1 friends]; getter 方法是nonnull
per1.friends = nil;
-
泛型
帶泛型的容器 (規定了容器中所存儲的類型 數組中添加其他類型會有警告 數組中取出的類型不再是id類型 泛型會改變類的一些方法 與之聲明的方法相同) (例如: 聲明一個可變數組內部存放的都是字符串 不能放其他類型)
NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"zhaohao", nil];
NSMutableArray<NSString *> *array = [NSMutableArray arrayWithObjects:@"123", nil];
// array1.lastObject. id 類型 沒有length屬性
// array.lastObject.length NSString類型 有length
[array addObject:@" "];
// [array addObject:@20];
// [array addObject:per1];
協變性 與 逆變性
#import <Foundation/Foundation.h>
// 自定義泛型聲明方式
// @interface 類名<泛型名> : 父類
// 自定義泛型 聲明后 泛型名 可以應用在屬性 方法中
// 泛型名等效于 類型修飾 (例: NSString *)
@interface Truck<__contravariant ObjectType> : NSObject
@property (nonatomic, strong) ObjectType firstObject;
- (void)addObject:(ObjectType)object;- (id)anyObject; @end #import "Truck.h" @implementation Truck - (void)addObject:(id)object { } - (id)anyObject { return nil; } // 方法的返回值 是一個數組 / 可變數組 - (__kindof NSArray *)returnArray { // 加上kindof 返回的是這 個類及這個類的子類 // return @[]; return [NSMutableArray array]; } @end main.m Truck *oneTruck = nil; Truck<NSArray *> *truck1 = nil; Truck<NSMutableArray *> *truck2 = nil; // 大類型(任意類型) 可賦值給任意類型 任意類型也可賦值給大類型 oneTruck = truck1; oneTruck = truck2; truck1 = oneTruck; truck2 = oneTruck; // __covariant 協變性 子類在給父類賦值時 自動強轉 truck1 = truck2; // __contravariant 逆變性 父類給子類賦值 自動強轉 truck2 = truck1;
-
__kindOf
#import <Foundation/Foundation.h>
@interface Truck<__contravariant ObjectType> : NSObject
@property (nonatomic, strong) ObjectType firstObject;
- (void)addObject:(ObjectType)object;- (id)anyObject; - (__kindof NSArray *)returnArray; @end #import "Truck.h" @implementation Truck - (void)addObject:(id)object { } - (id)anyObject { return nil; } // 方法的返回值 是一個數組 / 可變數組 - (__kindof NSArray *)returnArray { // 加上kindof 返回的是這 個類及這個類的子類 // return @[]; return [NSMutableArray array]; } @end main.m Truck *truckKindOf = [[Truck alloc] init]; // NSMutableArray *arr = (NSMutableArray *)[truckKindOf returnArray]; // 強轉 NSArray *arr1 = [truckKindOf returnArray];
7. UIView UIWindow
7.1 程序的組成結構
main.m (沒有他, 程序不能正常運行)
AppDelegate (程序的執行者 簽訂了UIApplicationDelegate協議 其中的所有協議方法都是可選的) AppDelegate就和一個普通的類 因為簽訂了協議 所以得到很多方法
ViewController(主要負責視圖管理)
Main.storyboard (主要負責視圖管理) LaunchScreen.storyboard (可視化管理 主要負責啟動頁)
Assets.xcassets (主要用來管理圖片素材) 原來叫 image.xcassets(x-code 7 之前)
LaunchScreen.storyboard
-
info.plist (工程配置文件)
模擬器 command + 1 ~ 5 (改變模擬器大小)
command + ← / → 橫屏 / 豎屏
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 給Window 添加顏色
self.window.backgroundColor = [UIColor whiteColor];
// 讓Window 顯示并成為主窗口
[_window makeKeyAndVisible];
// 從x-code7以后 必須設置 rootViewController
// 設置Windows主窗口
_window.rootViewController = [[ViewController alloc] init];// 設置坐標 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 270)]; view.backgroundColor = [UIColor cyanColor]; // 將view添加到window [self.window addSubview:view];
iOS坐標系 與 數學上的坐標系不同 y軸正方向是想下的 原點是屏幕的左上角
frame bounds center 都是UIView的屬性
frame (x, y, width, height) 指的是在他父視圖上的位置
bounds (x, y, width, height) 指的是視圖本身的坐標系 bounds x y 修改了本身的坐標 影響他的子視圖 bounds (x, y) 修改的是本身的大小 根據其中心點進行收縮或擴張 bounds的改變 不會影響其中心點
- 使用父類初始化方法(用來顯示文本)
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 270, 120)];
label.backgroundColor = [UIColor blackColor];
[_window addSubview:label]; - UILabel
text
label.text = @"美夢是個氣球 牽在手上 向往藍天 不管高低 不曾遠離 我視線 夢想是個諾言 寫在心上 明天你是否會想起 昨天你寫的日記 明天你是否還惦記曾經最愛哭的你 老師們都已想不起";
// textColor
label.textColor = [UIColor whiteColor];
// font
label.font = [UIFont systemFontOfSize:17]; // 默認17
// label.font = [UIFont boldSystemFontOfSize:10]; 加粗
// textAlignment 對齊方式 0 1 2 枚舉
label.textAlignment = 0;
// numberOfLines 不確定行數時 賦值 0
label.numberOfLines = 0; - UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 60, 30);
button.backgroundColor = [UIColor redColor];
[_window addSubview:button];
// 給button添加事件
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
// 添加文字
[button setTitle:@"確認" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; - UITextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 370, 270, 30)];
textField.backgroundColor = [UIColor redColor];
[_window addSubview:textField];
// 給輸入框賦初值
// textField.text = @"文字";
// 文字顏色
textField.textColor = [UIColor blackColor];
// borderStyle 邊緣
textField.placeholder = @"請輸入密碼";
textField.clearsOnBeginEditing = YES; // command + k
7.2 使用button 設計一個播放和暫停的功能
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[ViewController alloc] init];
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeSystem];
playButton.frame = CGRectMake(50, 100, 150, 30);
playButton.backgroundColor = [UIColor redColor];
[playButton setTitle:@"播放" forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playSong:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:playButton];
// 暫停
- (void)pasueSong:(UIButton *)pasueSong {
NSLog(@"暫停");
[pasueSong removeTarget:self action:@selector(pasueSong:) forControlEvents:UIControlEventTouchUpInside];
[pasueSong addTarget:self action:@selector(playSong:) forControlEvents:UIControlEventTouchUpInside];
[pasueSong setTitle:@"播放" forState:UIControlStateNormal];
}
- (void)playSong:(UIButton *)playButton {
NSLog(@"播放");
// 移除點擊事件
[playButton removeTarget:self action:@selector(playSong:) forControlEvents:UIControlEventTouchUpInside];
[playButton addTarget:self action:@selector(pasueSong:) forControlEvents:UIControlEventTouchUpInside];
[playButton setTitle:@"暫停" forState:UIControlStateNormal];
}
7.3 利用imageView設計簡單動畫
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
// 已經完成加載時 會走這個方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[ViewController alloc] init];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIImage *image = [UIImage imageNamed:@"BackGround.png"];
[imageView setImage:image];
[self.window addSubview:imageView];
UIImageView *flowerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(55, 100, 100, 100)];
NSMutableArray *flowerArray = [NSMutableArray array];
for (int i = 1; i < 19; i++) {
NSString *flowerName = [NSString stringWithFormat:@"flower%d.tiff", i];
UIImage *flowerImage = [UIImage imageNamed:flowerName];
[flowerArray addObject:flowerImage];
}
flowerImageView.animationImages = flowerArray;
flowerImageView.animationDuration = 2;
flowerImageView.animationRepeatCount = 0;
[flowerImageView startAnimating];
[self.window addSubview:flowerImageView];
UIImageView *guaImageView = [[UIImageView alloc] initWithFrame:CGRectMake(55, 200, 100, 100)];
NSMutableArray *guaArray = [NSMutableArray array];
for (int i = 1; i < 17; i++) {
NSString *guaName = [NSString stringWithFormat:@"gua%d.tiff", i];
UIImage *guaImage = [UIImage imageNamed:guaName];
[guaArray addObject:guaImage];
}
guaImageView.animationImages = guaArray;
guaImageView.animationDuration = 2;
guaImageView.animationRepeatCount = 0;
[guaImageView startAnimating];
[self.window addSubview:guaImageView];
UIImageView *plantsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(55, 300, 100, 100)];
NSMutableArray *plantsArray = [NSMutableArray array];
for (int i = 1; i < 10; i++) {
NSString *plantsName = [NSString stringWithFormat:@"plants%d.tiff", i];
UIImage *plantsImage = [UIImage imageNamed:plantsName];
[plantsArray addObject:plantsImage];
}
plantsImageView.animationImages = plantsArray;
plantsImageView.animationDuration = 2;
plantsImageView.animationRepeatCount = 0;
[plantsImageView startAnimating];
[self.window addSubview:plantsImageView];
return YES;
}
7.4 UIViewController
-
在UIViewController中程序執行的順序
// 初始化方法
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// 不要再這個方法中加載視圖
// 在這個方法中去初始化一些數據相關的內容
}
return self;
}// 加載ViewController中的view // UINavagationController UITableViewController在 loadView中給賦值的 - (void)loadView { [super loadView]; NSLog(@"loadView"); } // view已經加載 // 自定義視圖創建 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } // 下列方法 會執行多次+ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; }
7.5 模態跳轉頁面
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
PresentViewController *present = [[PresentViewController alloc] init];
// 模態出一個視圖控制器
present.modalTransitionStyle = 1; // 翻頁的效果
[self presentViewController:present animated:YES completion:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 返回上一頁 (讓模態出來的頁面消失)
[self dismissViewControllerAnimated:YES completion:nil];
}