问一下,iOS的动画策略?
有界面第一次加载,界面中间出现动画。
有上拉加载,界面下方出现动画。
一个界面的网络请求方法,应该是写在一个地方?
想了解 最佳实践。
动画都出现的原始代码如下:
[ZheProgressHUD showOnView: self.view ];// 界面中间开始转菊花 __weak typeof(self) weakSelf = self; // 开始 网络请求 [GetNetWorkDataMethod getDataWithParameters: parametersDiscover requestType:ZheNetworkingRequestTypeGet andWithURLStr: kArticleList userInfo: nil success:^(NSURLSessionDataTask *task, NSDictionary *responseObject, NSDictionary *userInfo) { NSDictionary * dictionaryArtList = responseObject; NSArray * artiLists = dictionaryArtList[@"Results"]; for (NSDictionary * dictionTemp in artiLists){ TagArticleListResultModel * listResultModel = [[TagArticleListResultModel alloc ] init ]; [listResultModel setValuesForKeysWithDictionary: dictionTemp ]; [self.articArray addObject: listResultModel ]; } dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.artiListTableView reloadData ]; [weakSelf.artiListTableView.mj_header endRefreshing ];//上面的菊花消失 [weakSelf.artiListTableView.mj_footer endRefreshing ];//下面的菊花消失 weakSelf.artiListTableView.mj_header.hidden = NO; weakSelf.artiListTableView.mj_footer.hidden = NO; [ZheProgressHUD hideAfterSuccessOnView: self.view];// 界面中间菊花消失 }); } exception:^(NSDictionary *responseObject, NSDictionary *userInfo) {} failure:^(NSError *error, NSDictionary *userInfo) { [ZheProgressHUD hideAfterFailOnView: self.view ];//界面中间菊花消失 }];
我自己的视线,就是引入变量,记录状态,根据不同的状态判断,然后加与不加动画。
感觉很原始,很直观,有点Low
解决方案
变量 无法标记吧?你无法确定网络请求返回的时机。
两种方法:
如果真要确定是上中下哪个动画,可以加个枚举嘛:
typedef NS_ENUM(NSInteger, Animation) { AnimationTop, AnimationCenter, AnimationBottom};- (void)request:(id)para type:(Animation)type { [GetNetWorkDataMethod getDataWithParameters: parametersDiscover requestType:ZheNetworkingRequestTypeGet andWithURLStr: kArticleList userInfo: nil success:^(NSURLSessionDataTask *task, NSDictionary *responseObject, NSDictionary *userInfo) { if (type == AnimationTop) { [weakSelf.artiListTableView.mj_header endRefreshing ]; } } exception:^(NSDictionary *responseObject, NSDictionary *userInfo) {} failure:^(NSError *error, NSDictionary *userInfo) { }];}use: [self request:@[...] type:AnimationTop];
如果你觉得加个参数和枚举不美观,可以用回调:
- (void)request:(id)para compelete:(dispatch_block_t)compelete { [GetNetWorkDataMethod getDataWithParameters: parametersDiscover requestType:ZheNetworkingRequestTypeGet andWithURLStr: kArticleList userInfo: nil success:^(NSURLSessionDataTask *task, NSDictionary *responseObject, NSDictionary *userInfo) { ... compelete ? compelete : 0; } exception:^(NSDictionary *responseObject, NSDictionary *userInfo) {} failure:^(NSError *error, NSDictionary *userInfo) { }];}// use: 上部动画[self request:@[...] compelete:^{ // [self.artiListTableView.mj_header endRefreshing ];}];