1. Json簡單介紹
json和plist本質(zhì)都是一堆以一定格式編寫的字符串
json和plist都可以用來表示數(shù)據(jù)信息
json '{}' 表示字典
json '[]' 表示數(shù)組
Json在線格式化查看器:
http://www.jsoneditoronline.org/
http://www.bejson.com/
http://www.sojson.com/
2. Json數(shù)據(jù)簡單解析
解析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];
// 通過data轉(zhuǎn) 字典
NSArray* dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
3. 數(shù)組泛型介紹
寫法:聲明數(shù)組的時候 在 NSArray 后面 (在*之前) , 寫一個 <希望放的類型>
兩點好處
1.這個數(shù)組 只放<希望放的類型>對象,不放其他的,如果放了會提示(警告)
2.這個數(shù)組獲取的某一個元素能夠直接'點'出來'<希望放的類型>'的屬性
@property(strong,nonatomic)NSArray ?*spus;
4. 轉(zhuǎn)模型數(shù)據(jù)中使用setValues中的一對兒方法
在字典中的某個key 有相對應(yīng)的屬性,那么會走 setValue:forKey: 方法
在字典中的某個key 如果在模型中沒有相對應(yīng)的屬性,那么會走 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一定要寫。不然上面的方法全都白寫了,會繼續(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
通過 sd_setImageUrl的方法 進行加載
報錯http的處理方法,控制器會提示錯誤, ATS。iOS9.0 之后默認是不支持http了,官方建議使用https。所以需要開啟支持http。
需要在infoPlist中配置一個叫做 App Transport Security Settings 的key,這是一個字典,下面再加一個 allow 開的bool類型的 YES即可
圖片還是加載不出來,需要把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 讓屏幕滾動到指定的cell
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
6.2 獲取屏幕上出現(xiàn)的cell信息
view有一個屬性,可以獲取屏幕上出現(xiàn)的cell信息。返回值是一個數(shù)組。
@property (nonatomic, readonly, nullable) NSArray *indexPathsForVisibleRows;
利用這個屬性,可以獲取到屏幕上出現(xiàn)的第一個cell的indexPath
//獲取屏幕上出現(xiàn)的cell的第一個indexpath
NSIndexPath *firstIndexPath = [self.categoryFoodView indexPathsForVisibleRows].firstObject;
6.3 選中指定的indexpath的cell
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
7. 繪制拋物線動畫
步驟:
獲取拋物線的起止點
創(chuàng)建關(guān)鍵幀動畫
創(chuàng)建bezierPath
將path移動至起點
添加帶控制點的拋物線
將path賦值給動畫路徑
可以給view設(shè)置一個額外的的屬性,方便能夠找到這個view
- (void)startAnimation{
// 獲取加號的坐標
CGPoint startPoint = [categoryFoodCell convertPoint:shoppingCartBtn.center toView:self.view];
// 定義動畫的結(jié)束點坐標
CGPoint endPoint = [self.cartIconView convertPoint:self.cartIconView.center toView:self.view];
// 創(chuàng)建動畫小紅點
UIImageView *redPointView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_common_point"]];
[self.view addSubview:redPointView];
redPointView.center = startPoint;
// 設(shè)置動畫
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
//繪制拋物線,添加控制點
[path addQuadCurveToPoint:endPoint controlPoint:CGPointMake(startPoint.x - 100, startPoint.y - 100)];
keyAnimation.path = path.CGPath;
keyAnimation.delegate = self;
keyAnimation.removedOnCompletion = NO;
// 設(shè)置動畫持續(xù)時間
keyAnimation.duration = 2;
keyAnimation.fillMode = kCAFillModeForwards;
// 給小紅點設(shè)置一個額外的標示賦,用于動畫播放結(jié)束后移除
[keyAnimation setValue:redPointView forKey:@"tagOfRedPointView"];
[redPointView.layer addAnimation:keyAnimation forKey:@"shoppingCart"];
}
//動畫播放結(jié)束
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
// 將小紅點的view先取出來,然后從父控件刪除
UIImageView *imageView = [anim valueForKey:@"tagOfRedPointView"];
[imageView removeFromSuperview];
// 加這句話的意思是讓imageview立即從內(nèi)存中釋放掉。不然等待ARC釋放,還需要一段時間
imageView = nil;
}
8. 自定義控件(集成UIControl)
如果之前是某一個繼承view的視圖,如果想要監(jiān)聽,可以直接把這個view改成繼承自UIControl
通過addtarget的方法 進行監(jiān)聽.
在需要使用的時候,發(fā)送一個事件(sendActionsForControlEvents)