1. Json簡(jiǎn)單介紹
json和plist本質(zhì)都是一堆以一定格式編寫的字符串
json和plist都可以用來(lái)表示數(shù)據(jù)信息
json '{}' 表示字典
json '[]' 表示數(shù)組
Json在線格式化查看器:
http://www.jsoneditoronline.org/
http://www.bejson.com/
http://www.sojson.com/
2. Json數(shù)據(jù)簡(jiǎn)單解析
解析json和解析plist基本一致
plist中有一句 可以直接把文件的路徑 轉(zhuǎn)化成字典或數(shù)組
json中是兩句 先把文件轉(zhuǎn)成 data 再轉(zhuǎn)成字典或數(shù)組
關(guān)鍵類 : NSJSONSerialization
// 獲取文件路徑
NSURL* path = [[NSBundle mainBundle] URLForResource:@"test.json" withExtension:nil];
//根據(jù)文件 轉(zhuǎn)化成 NSData
NSData* data = [NSData dataWithContentsOfURL:path];
// 通過(guò)data轉(zhuǎn) 字典
NSArray* dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
3. 數(shù)組泛型介紹
寫法:聲明數(shù)組的時(shí)候 在 NSArray 后面 (在*之前) , 寫一個(gè) <希望放的類型>
兩點(diǎn)好處
1.這個(gè)數(shù)組 只放<希望放的類型>對(duì)象,不放其他的,如果放了會(huì)提示(警告)
2.這個(gè)數(shù)組獲取的某一個(gè)元素能夠直接'點(diǎn)'出來(lái)'<希望放的類型>'的屬性
@property(strong,nonatomic)NSArray ?*spus;
4. 轉(zhuǎn)模型數(shù)據(jù)中使用setValues中的一對(duì)兒方法
在字典中的某個(gè)key 有相對(duì)應(yīng)的屬性,那么會(huì)走 setValue:forKey: 方法
在字典中的某個(gè)key 如果在模型中沒(méi)有相對(duì)應(yīng)的屬性,那么會(huì)走 setValue:forUndefinedKey: 方法
- (void)setValue:(id)value forKey:(NSString *)key{
? ?if ([key isEqualToString:@"spus"]) {
? ? ? ?NSArray *temArray = value;
? ? ? ?NSMutableArray *arrM = [NSMutableArray array];
? ? ? ?for (NSDictionary *dict in temArray) {
? ? ? ? ? ?GMHomeFoodSpus *homeFoodSpus = [GMHomeFoodSpus foodSpusWithDict:dict];
? ? ? ? ? ?[arrM addObject:homeFoodSpus];
? ? ? ?}
? ? ? ?[super setValue:arrM forKey:key];
? ? ? ?return;
//**這里的return一定要寫灶伊。不然上面的方法全都白寫了剪芍,會(huì)繼續(xù)執(zhí)行下面的方法。**
? ?}
? ?[super setValue:value forKey:key];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
? ?if ([key isEqualToString:@"description"]) {
? ? ? ?self.discountDescription = value;
? ?}
}
5. 使用SDWebImage加載圖片
步驟:
導(dǎo)入SDWebImage
通過(guò) sd_setImageUrl的方法 進(jìn)行加載
報(bào)錯(cuò)http的處理方法抡爹,控制器會(huì)提示錯(cuò)誤, ATS嘴纺。iOS9.0 之后默認(rèn)是不支持http了,官方建議使用https。所以需要開啟支持http。
需要在infoPlist中配置一個(gè)叫做 App Transport Security Settings 的key,這是一個(gè)字典,下面再加一個(gè) allow 開的bool類型的 YES即可
圖片還是加載不出來(lái),需要把picture屬性中的后綴名 刪除掉
Alt text
// 使用框架中的方法锨能,將圖片的后綴wbp刪除掉
NSString *imageName = [self.foodSpusData.picture stringByDeletingPathExtension];
// 使用框架中的方法,設(shè)置圖片及占位圖片
[self.pictureView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:@"img_food_loading"]];
6. tableView里面的小方法
6.1 讓屏幕滾動(dòng)到指定的cell
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
6.2 獲取屏幕上出現(xiàn)的cell信息
view有一個(gè)屬性芍耘,可以獲取屏幕上出現(xiàn)的cell信息。返回值是一個(gè)數(shù)組熄阻。
@property (nonatomic, readonly, nullable) NSArray *indexPathsForVisibleRows;
利用這個(gè)屬性斋竞,可以獲取到屏幕上出現(xiàn)的第一個(gè)cell的indexPath
//獲取屏幕上出現(xiàn)的cell的第一個(gè)indexpath
NSIndexPath *firstIndexPath = [self.categoryFoodView indexPathsForVisibleRows].firstObject;
6.3 選中指定的indexpath的cell
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
7. 繪制拋物線動(dòng)畫
步驟:
獲取拋物線的起止點(diǎn)
創(chuàng)建關(guān)鍵幀動(dòng)畫
創(chuàng)建bezierPath
將path移動(dòng)至起點(diǎn)
添加帶控制點(diǎn)的拋物線
將path賦值給動(dòng)畫路徑
可以給view設(shè)置一個(gè)額外的的屬性,方便能夠找到這個(gè)view
- (void)startAnimation{
// 獲取加號(hào)的坐標(biāo)
CGPoint startPoint = [categoryFoodCell convertPoint:shoppingCartBtn.center toView:self.view];
// 定義動(dòng)畫的結(jié)束點(diǎn)坐標(biāo)
CGPoint endPoint = [self.cartIconView convertPoint:self.cartIconView.center toView:self.view];
// 創(chuàng)建動(dòng)畫小紅點(diǎn)
UIImageView *redPointView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_common_point"]];
[self.view addSubview:redPointView];
redPointView.center = startPoint;
// 設(shè)置動(dòng)畫
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
//繪制拋物線秃殉,添加控制點(diǎn)
[path addQuadCurveToPoint:endPoint controlPoint:CGPointMake(startPoint.x - 100, startPoint.y - 100)];
keyAnimation.path = path.CGPath;
keyAnimation.delegate = self;
keyAnimation.removedOnCompletion = NO;
// 設(shè)置動(dòng)畫持續(xù)時(shí)間
keyAnimation.duration = 2;
keyAnimation.fillMode = kCAFillModeForwards;
// 給小紅點(diǎn)設(shè)置一個(gè)額外的標(biāo)示賦坝初,用于動(dòng)畫播放結(jié)束后移除
[keyAnimation setValue:redPointView forKey:@"tagOfRedPointView"];
[redPointView.layer addAnimation:keyAnimation forKey:@"shoppingCart"];
}
//動(dòng)畫播放結(jié)束
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
// 將小紅點(diǎn)的view先取出來(lái),然后從父控件刪除
UIImageView *imageView = [anim valueForKey:@"tagOfRedPointView"];
[imageView removeFromSuperview];
// 加這句話的意思是讓imageview立即從內(nèi)存中釋放掉钾军。不然等待ARC釋放鳄袍,還需要一段時(shí)間
imageView = nil;
}
8. 自定義控件(集成UIControl)
如果之前是某一個(gè)繼承view的視圖,如果想要監(jiān)聽,可以直接把這個(gè)view改成繼承自UIControl
通過(guò)addtarget的方法 進(jìn)行監(jiān)聽.
在需要使用的時(shí)候,發(fā)送一個(gè)事件(sendActionsForControlEvents)